Example usage for org.springframework.mail.javamail MimeMessageHelper setSubject

List of usage examples for org.springframework.mail.javamail MimeMessageHelper setSubject

Introduction

In this page you can find the example usage for org.springframework.mail.javamail MimeMessageHelper setSubject.

Prototype

public void setSubject(String subject) throws MessagingException 

Source Link

Document

Set the subject of the message, using the correct encoding.

Usage

From source file:uk.org.rbc1b.roms.scheduled.EmailScheduledService.java

/**
 * Generates and sends out emails.//from   ww  w. ja v a 2s .  co  m
 * @param email the email to send out
 * @throws MessagingException if problem with creating an email sender
 */
private void sendEmail(Email email) throws MessagingException {
    MimeMessage mimeMessage = this.mailGateway.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    if (email.getReplyTo() != null) {
        helper.setReplyTo(email.getReplyTo());
    }
    helper.setTo(email.getRecipient());
    if (email.getCc() != null) {
        helper.setCc(email.getCc());
    }
    helper.setSubject(email.getSubject());
    helper.setText(email.getText(), email.isHtml());
    this.mailGateway.send(mimeMessage);
}

From source file:cz.zcu.kiv.eegdatabase.webservices.rest.user.UserServiceImpl.java

/**
 * Method for sending registration confirmation email.
 *
 * @param registrationPath path to registration confirmation page
 * @param plainPassword    password in plain text
 * @param user             created user object
 * @param locale           locale/*from  w  w w  .  j  ava 2s  .  c  o  m*/
 * @throws MailException      error during sending mail
 * @throws MessagingException error during sending mail
 */
private void sendRegistrationConfirmMail(String registrationPath, String plainPassword, Person user,
        Locale locale) throws MailException, MessagingException {
    log.debug("Creating email content");
    StringBuilder sb = new StringBuilder();
    String login = "<b>" + user.getUsername() + "</b>";

    sb.append("<html><body>");
    sb.append("<h4>");
    sb.append(messageSource.getMessage("registration.email.welcome", null, locale));
    sb.append("</h4>");
    sb.append("<p>");
    sb.append(messageSource.getMessage("registration.email.body.yourLogin", new String[] { login }, locale));
    sb.append("</p>");
    sb.append("<p>");
    sb.append(messageSource.getMessage("registration.email.body.yourPassword", new String[] { plainPassword },
            locale));
    sb.append("</p>");
    sb.append("<p>");
    sb.append(messageSource.getMessage("registration.email.body.clickToRegister", null, locale));
    sb.append("<br/>");

    String confirmURL = registrationPath + user.getAuthenticationHash();
    sb.append("<a href=\"").append(confirmURL).append("\">").append(confirmURL).append("</a>");
    sb.append("</p>");
    sb.append("</body></html>");

    String emailSubject = messageSource.getMessage("registration.email.subject", null, locale);
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
    message.setFrom(mailMessage.getFrom());
    message.setTo(user.getEmail());
    message.setSubject(emailSubject);
    message.setText(sb.toString(), true);
    mailSender.send(mimeMessage);
}

From source file:com.gcrm.util.mail.MailService.java

public void sendSimpleMail(String toAddress) throws Exception {
    List<EmailSetting> emailSettings = baseService.getAllObjects(EmailSetting.class.getSimpleName());
    EmailSetting emailSetting = null;//  w  w  w . ja  va2s  .  c o m
    if (emailSettings != null && emailSettings.size() > 0) {
        emailSetting = emailSettings.get(0);
    } else {
        return;
    }
    Session mailSession = this.createSmtpSession(emailSetting);
    if (mailSession != null) {
        Transport transport = mailSession.getTransport();
        MimeMessage msg = new MimeMessage(mailSession);
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
        helper.setFrom(emailSetting.getFrom_address());
        helper.setTo(toAddress);
        helper.setSubject("Test Mail From " + emailSetting.getFrom_name());
        helper.setText("This is test mail from " + emailSetting.getFrom_name(), true);
        transport.connect();
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
    }
}

From source file:com.gcrm.util.mail.MailService.java

public void sendSystemSimpleMail(String toAddress, String subject, String text) throws Exception {
    List<EmailSetting> emailSettings = baseService.getAllObjects(EmailSetting.class.getSimpleName());
    EmailSetting emailSetting = null;/*from w w w .j a va2s . c  o m*/
    if (emailSettings != null && emailSettings.size() > 0) {
        emailSetting = emailSettings.get(0);
    } else {
        return;
    }
    Session mailSession = this.createSmtpSession(emailSetting);
    if (mailSession != null) {
        Transport transport = mailSession.getTransport();
        MimeMessage msg = new MimeMessage(mailSession);
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
        helper.setFrom(emailSetting.getFrom_address());
        helper.setTo(toAddress);
        helper.setSubject(subject);
        helper.setText(text, true);
        transport.connect();
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
    }
}

From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java

public String sendMailVLClassification(final String period, final String mailDate, final String protocolCode,
        final String recipientName, final String recipientAddress, final String recipientCity,
        final String recipientPhone, final String recipientEmail, final String practice_id,
        final String position, final String score, final String determinationCode,
        final String determinationDate, final String alboDate, final String expirationDate, final String phase,
        final String ef_period, final String ef_category, final String ef_tool, final String classificationUrl,
        final String respName, final String subject, final Locale locale, final MailImage logoImage,
        final MailImage footerImage, final int type) throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("imagelogoMyweb", logoImage.getImageName());
    ctx.setVariable("mailDate", mailDate);
    ctx.setVariable("period", period);
    ctx.setVariable("protCode", protocolCode);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("address", recipientAddress);
    ctx.setVariable("city", recipientCity);
    ctx.setVariable("phone", recipientPhone);
    ctx.setVariable("detCode", determinationCode);
    ctx.setVariable("detDate", determinationDate);
    ctx.setVariable("alboDate", alboDate);
    ctx.setVariable("expDate", expirationDate);
    ctx.setVariable("practice_id", practice_id);
    ctx.setVariable("position", position);
    ctx.setVariable("score", score);
    ctx.setVariable("phase", phase);
    ctx.setVariable("ef_period", ef_period);
    ctx.setVariable("ef_category", ef_category);
    ctx.setVariable("ef_tool", ef_tool);
    ctx.setVariable("classification_url", classificationUrl);
    ctx.setVariable("respName", respName);
    ctx.setVariable("subscriptionDate", new Date());
    //ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    ctx.setVariable("text", subject);
    ctx.setVariable("imagefooterVallag", footerImage.getImageName());

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
    message.setSubject("Graduatoria Edilizia Abitativa");
    //message.setFrom("thymeleaf@example.com");
    message.setFrom("myweb.edilizia@comunitadellavallagarina.tn.it");
    //message.setFrom("myweb-graduatoria@smartcommunitylab.it");
    message.setTo(recipientEmail);//w  w w  .  jav  a 2 s . c o m

    // Create the HTML body using Thymeleaf
    if (type == 1) {
        final String htmlContent = this.templateEngine.process("email-vallagarina.html", ctx);
        message.setText(htmlContent, true /* isHtml */);
    } else {
        final String htmlContent = this.templateEngine.process("email-vallagarina-final.html", ctx);
        message.setText(htmlContent, true /* isHtml */);
    }

    // Add the inline titles image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageLogo = new ByteArrayResource(logoImage.getImageByte());
    message.addInline(logoImage.getImageName(), imageLogo, logoImage.getImageType());

    // Add the inline footer image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageFooter = new ByteArrayResource(footerImage.getImageByte());
    message.addInline(footerImage.getImageName(), imageFooter, footerImage.getImageType());

    // Add the attachment
    //final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes);
    //message.addAttachment(
    //        attachmentFileName, attachmentSource, attachmentContentType);

    // Send mail
    this.mailSender.send(mimeMessage);

    return recipientName + "OK";
}

From source file:cherry.foundation.mail.MailSendHandlerImpl.java

private void send(final SimpleMailMessage msg, final AttachmentPreparator preparator) {
    if (preparator == null) {
        mailSender.send(msg);/*from  www . j a  v a 2  s  .  co m*/
    } else {
        mailSender.send(new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setTo(msg.getTo());
                helper.setCc(msg.getCc());
                helper.setBcc(msg.getBcc());
                helper.setFrom(msg.getFrom());
                helper.setSubject(msg.getSubject());
                helper.setText(msg.getText());
                preparator.prepare(new Attachment(helper));
            }
        });
    }
}

From source file:com.registryKit.user.emailMessageManager.java

@Async
public void sendEmail(mailMessage messageDetails) throws Exception {

    MimeMessage msg = mailSender.createMimeMessage();

    try {// www  .ja v a 2  s  .  co  m

        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(messageDetails.getfromEmailAddress());
        helper.setTo(messageDetails.gettoEmailAddress());

        if (messageDetails.getccEmailAddress() != null) {
            helper.setCc(messageDetails.getccEmailAddress());
        }

        helper.setSubject(messageDetails.getmessageSubject());

        helper.setText("", messageDetails.getmessageBody());
        helper.setReplyTo(messageDetails.getfromEmailAddress());

        mailSender.send(msg);

    } catch (Exception e) {
        throw new Exception(e);
    }

}

From source file:com.ut.healthelink.service.impl.emailMessageManagerImpl.java

@Async
public void sendEmail(mailMessage messageDetails) throws Exception {

    MimeMessage msg = mailSender.createMimeMessage();

    try {//from   w w  w .  j  a  va 2  s .c  o  m
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(messageDetails.getfromEmailAddress());
        helper.setTo(messageDetails.gettoEmailAddress());

        if (messageDetails.getccEmailAddress() != null) {
            helper.setCc(messageDetails.getccEmailAddress());
        }

        helper.setSubject(messageDetails.getmessageSubject());

        helper.setText("", messageDetails.getmessageBody());
        helper.setReplyTo(messageDetails.getfromEmailAddress());

        mailSender.send(msg);

    } catch (Exception e) {
        throw new Exception(e);
    }

}

From source file:com.epam.ta.reportportal.util.email.EmailService.java

/**
 * Finish launch notification//from   w  w  w.j a  va2 s  . co  m
 *
 * @param recipients List of recipients
 * @param url        ReportPortal URL
 * @param launch     Launch
 */
public void sendLaunchFinishNotification(final String[] recipients, final String url, final Launch launch,
        final Project.Configuration settings) {
    String subject = String.format(FINISH_LAUNCH_EMAIL_SUBJECT, launch.getName(), launch.getNumber());
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "utf-8");
        message.setSubject(subject);
        message.setTo(recipients);
        setFrom(message);

        Map<String, Object> email = new HashMap<>();
        /* Email fields values */
        email.put("name", launch.getName());
        email.put("number", String.valueOf(launch.getNumber()));
        email.put("description", launch.getDescription());
        email.put("url", url);

        /* Launch execution statistics */
        email.put("total", launch.getStatistics().getExecutionCounter().getTotal().toString());
        email.put("passed", launch.getStatistics().getExecutionCounter().getPassed().toString());
        email.put("failed", launch.getStatistics().getExecutionCounter().getFailed().toString());
        email.put("skipped", launch.getStatistics().getExecutionCounter().getSkipped().toString());

        /* Launch issue statistics global counters */
        email.put("productBugTotal", launch.getStatistics().getIssueCounter().getProductBugTotal().toString());
        email.put("automationBugTotal",
                launch.getStatistics().getIssueCounter().getAutomationBugTotal().toString());
        email.put("systemIssueTotal",
                launch.getStatistics().getIssueCounter().getSystemIssueTotal().toString());
        email.put("noDefectTotal", launch.getStatistics().getIssueCounter().getNoDefectTotal().toString());
        email.put("toInvestigateTotal",
                launch.getStatistics().getIssueCounter().getToInvestigateTotal().toString());

        /* Launch issue statistics custom sub-types */
        if (launch.getStatistics().getIssueCounter().getProductBug().entrySet().size() > 1) {
            Map<StatisticSubType, String> pb = new LinkedHashMap<>();
            launch.getStatistics().getIssueCounter().getProductBug().forEach((k, v) -> {
                if (!k.equalsIgnoreCase(IssueCounter.GROUP_TOTAL))
                    pb.put(settings.getByLocator(k), v.toString());
            });
            email.put("pbInfo", pb);
        }
        if (launch.getStatistics().getIssueCounter().getAutomationBug().entrySet().size() > 1) {
            Map<StatisticSubType, String> ab = new LinkedHashMap<>();
            launch.getStatistics().getIssueCounter().getAutomationBug().forEach((k, v) -> {
                if (!k.equalsIgnoreCase(IssueCounter.GROUP_TOTAL))
                    ab.put(settings.getByLocator(k), v.toString());
            });
            email.put("abInfo", ab);
        }
        if (launch.getStatistics().getIssueCounter().getSystemIssue().entrySet().size() > 1) {
            Map<StatisticSubType, String> si = new LinkedHashMap<>();
            launch.getStatistics().getIssueCounter().getSystemIssue().forEach((k, v) -> {
                if (!k.equalsIgnoreCase(IssueCounter.GROUP_TOTAL))
                    si.put(settings.getByLocator(k), v.toString());
            });
            email.put("siInfo", si);
        }
        if (launch.getStatistics().getIssueCounter().getNoDefect().entrySet().size() > 1) {
            Map<StatisticSubType, String> nd = new LinkedHashMap<>();
            launch.getStatistics().getIssueCounter().getNoDefect().forEach((k, v) -> {
                if (!k.equalsIgnoreCase(IssueCounter.GROUP_TOTAL))
                    nd.put(settings.getByLocator(k), v.toString());
            });
            email.put("ndInfo", nd);
        }
        if (launch.getStatistics().getIssueCounter().getToInvestigate().entrySet().size() > 1) {
            Map<StatisticSubType, String> ti = new LinkedHashMap<>();
            launch.getStatistics().getIssueCounter().getToInvestigate().forEach((k, v) -> {
                if (!k.equalsIgnoreCase(IssueCounter.GROUP_TOTAL))
                    ti.put(settings.getByLocator(k), v.toString());
            });
            email.put("tiInfo", ti);
        }

        String text = templateEngine.merge("finish-launch-template.vm", email);
        message.setText(text, true);
        message.addInline("logoimg", new UrlResource(getClass().getClassLoader().getResource(LOGO)));
    };
    this.send(preparator);
}

From source file:com.ushahidi.swiftriver.core.mail.EmailHelper.java

private MimeMessagePreparator getMimeMessagePreparator(final User user, final String subject,
        final String mailBody) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper mimeHelper = new MimeMessageHelper(mimeMessage, true);
            mimeHelper.setFrom(senderAddress);
            mimeHelper.setTo(user.getEmail());
            mimeHelper.setReplyTo(senderAddress);
            mimeHelper.setSubject(subject);
            mimeHelper.setText(mailBody, true);
        }//from  w  w w.  ja va2 s .c om
    };

    return preparator;
}