Posted By

arunpjohny on 10/29/09


Tagged

spring javamail


Versions (?)

Send mail using Spring JavaMailSender


 / Published in: Java
 

  1. public void mail(final String fromAddress, final String toAddress,
  2. final String cc, final String subject, final String body,
  3. Boolean html, final Map<String, File> attach)
  4. throws MailerException {
  5. final boolean isHtml = BooleanUtils.toBooleanDefaultIfNull(html, false);
  6. final boolean multipart = attach != null && attach.size() > 0;
  7. MimeMessagePreparator preparator = new MimeMessagePreparator() {
  8. public void prepare(MimeMessage mimeMessage) throws Exception {
  9. MimeMessageHelper messageHelper = new MimeMessageHelper(
  10. mimeMessage, multipart, "UTF-8");
  11. messageHelper.setSubject(subject);
  12. messageHelper.setTo(toAddress);
  13. if (!StringUtils.isEmpty(cc)) {
  14. messageHelper.setCc(cc.split(","));
  15. }
  16. // messageHelper.setBcc(bccEmail);
  17. messageHelper.setFrom(fromAddress);
  18. messageHelper.setText(body, isHtml);
  19.  
  20. if (attach != null) {
  21. for (Map.Entry<String, File> entry : attach.entrySet()) {
  22. messageHelper.addAttachment(entry.getKey(), entry
  23. .getValue());
  24. }
  25. }
  26. }
  27. };
  28.  
  29. try {
  30. this.mailSender.send(preparator);
  31. } catch (MailException e) {
  32. log.error("Error while sending mail", e);
  33. throw new MailException("Unable to send mail because of a unknown error, please try again", e);
  34. }
  35. }

Report this snippet  

You need to login to post a comment.