Search in sources :

Example 91 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project tutorials-java by Artister.

the class ApplicationTests method sendTemplateMail.

@Test
public void sendTemplateMail() throws Exception {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setFrom(from);
    helper.setTo(from);
    helper.setSubject("主题:模板邮件");
    Map<String, Object> model = new HashMap();
    model.put("name", "world");
    Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.ftl");
    String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    helper.setText(text, true);
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Template(freemarker.template.Template) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 92 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project irida by phac-nml.

the class EmailControllerImpl method sendNCBIUploadExceptionEmail.

/**
 * {@inheritDoc}
 */
@Override
public void sendNCBIUploadExceptionEmail(String adminEmailAddress, Exception rootCause, Long submissionId) throws MailSendException {
    final MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
    try {
        message.setSubject("IRIDA NCBI Upload Exception: " + rootCause.getMessage());
        message.setTo(adminEmailAddress);
        message.setFrom(serverEmail);
        message.setText("An exeption occurred when attempting to communicate with NCBI's SRA.  Submission " + submissionId + " had an error:" + rootCause);
        javaMailSender.send(mimeMessage);
    } catch (final MessagingException e) {
        logger.error("Error trying to send exception email.", e);
        throw new MailSendException("Failed to send e-mail for NCBI SRA related-exception.", e);
    }
}
Also used : MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 93 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project irida by phac-nml.

the class EmailControllerImpl method sendWelcomeEmail.

/**
 * {@inheritDoc}
 */
@Override
public void sendWelcomeEmail(User user, User sender, PasswordReset passwordReset) throws MailSendException {
    logger.debug("Sending user creation email to " + user.getEmail());
    Locale locale = LocaleContextHolder.getLocale();
    final Context ctx = new Context(locale);
    ctx.setVariable("ngsEmail", serverEmail);
    ctx.setVariable("serverURL", serverURL);
    ctx.setVariable("creator", sender);
    ctx.setVariable("user", user);
    ctx.setVariable("passwordReset", passwordReset);
    try {
        final MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
        message.setSubject(messageSource.getMessage("email.welcome.subject", null, locale));
        message.setFrom(serverEmail);
        message.setTo(user.getEmail());
        final String htmlContent = templateEngine.process(WELCOME_TEMPLATE, ctx);
        message.setText(htmlContent, true);
        javaMailSender.send(mimeMessage);
    } catch (final Exception e) {
        logger.error("User creation email failed to send", e);
        throw new MailSendException("Failed to send e-mail when creating user account.", e);
    }
}
Also used : Locale(java.util.Locale) Context(org.thymeleaf.context.Context) MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MessagingException(javax.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException)

Example 94 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project irida by phac-nml.

the class EmailControllerImpl method sendFilesystemExceptionEmail.

/**
 * {@inheritDoc}
 */
@Override
public void sendFilesystemExceptionEmail(final String adminEmailAddress, final Exception rootCause) throws MailSendException {
    final MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
    try {
        message.setSubject("IRIDA Storage Exception: " + rootCause.getMessage());
        message.setTo(adminEmailAddress);
        message.setFrom(serverEmail);
        message.setText("An exeption related to storage has occurred that requires your attention, stack as follows: " + rootCause);
        javaMailSender.send(mimeMessage);
    } catch (final MessagingException e) {
        logger.error("Error trying to send exception email. (Ack.)", e);
        throw new MailSendException("Failed to send e-mail for storage related-exception.", e);
    }
}
Also used : MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 95 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project ma-core-public by infiniteautomation.

the class EmailSender method createPreparator.

public MimeMessagePreparator createPreparator(final InternetAddress from, final InternetAddress replyTo, final InternetAddress[] to, final InternetAddress[] cc, final InternetAddress[] bcc, final String subject, final EmailContent content) throws MailException {
    return new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, content.isMultipart(), content.getEncoding());
            helper.setFrom(from);
            if (replyTo != null)
                helper.setReplyTo(replyTo);
            helper.setTo(to);
            if (cc != null)
                helper.setCc(cc);
            if (bcc != null)
                helper.setBcc(bcc);
            // Ensure that line breaks in the subject are removed.
            String sub;
            if (subject == null)
                sub = "";
            else {
                sub = subject.replaceAll("\\r", "");
                sub = sub.replaceAll("\\n", "");
            }
            helper.setSubject(sub);
            if (content.getHtmlContent() == null)
                helper.setText(content.getPlainContent(), false);
            else if (content.getPlainContent() == null)
                helper.setText(content.getHtmlContent(), true);
            else
                helper.setText(content.getPlainContent(), content.getHtmlContent());
            for (EmailAttachment att : content.getAttachments()) att.attach(helper);
            for (EmailInline inline : content.getInlines()) inline.attach(helper);
        }
    };
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5