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);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
};
}
Aggregations