/* * Copyright 2009 CIPRES project. http://www.phylo.org/ * All Rights Reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for educational, research and non-profit purposes, without * fee, and without a written agreement is hereby granted, provided that the * above copyright notice, this paragraph and the following two paragraphs * appear in all copies. * * Permission to incorporate this software into commercial products may be * obtained by contacting us: * http://www.phylo.org/contactUs.html * * The software program and documentation are supplied "as is". In no event * shall the CIPRES project be liable to any party for direct, indirect, * special, incidental, or consequential damages, including lost profits, * arising out of the use of this software and its documentation, even if * the CIPRES project has been advised of the possibility of such damage. * The CIPRES project specifically disclaims any warranties, including, but * not limited to, the implied warranties of merchantability and fitness for * a particular purpose. The CIPRES project has no obligations to provide * maintenance, support, updates, enhancements, or modifications. */ package org.cipres.utils; import java.util.Map; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import freemarker.template.Template; /** * MailService.java * * @author Lucie Chan */ public class MailService { private final Logger logger = Logger.getLogger(MailService.class); protected JavaMailSender mailSender; private FreeMarkerConfigurer mailTemplateEngine; private SimpleMailMessage mailMessage; public void setMailTemplateEngine ( FreeMarkerConfigurer mailTemplateEngine ) { this.mailTemplateEngine = mailTemplateEngine; } public void setMailSender ( JavaMailSender mailSender ) { this.mailSender = mailSender; } public void setMailMessage ( SimpleMailMessage mailMessage ) { this.mailMessage = mailMessage; } /** * Send MIME message (both text and htm) using freemarker templates * * @param to: recipient of message * @param templatePrefix: the name of the templates used * @param model: contains values to be replaced in templates at runtime * * @throws Exception: exception throws by this call */ public void sendMimeMessage ( String to, String from, String subject, String templatePrefix, Map model ) throws Exception { logger.debug("BEGIN: sendMimeMessage(String, String, String, String, Map)::void"); String textcontent = generateEmailContent(templatePrefix + "-text.ftl", model); String htmlcontent = generateEmailContent(templatePrefix + "-html.ftl", model); logger.debug(String.format("TEXT email content:\n%s", textcontent)); logger.debug(String.format("HTML email content:\n%s", htmlcontent)); MimeMessage mimeMsg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "utf-8"); helper.setTo(to); if (from != null) { helper.setFrom(from); } else { helper.setFrom(mailMessage.getFrom()); } if (subject != null) { helper.setSubject(subject); } else { helper.setSubject(mailMessage.getSubject()); } helper.setText(textcontent, htmlcontent); mailSender.send(mimeMsg); logger.debug("END: sendMimeMessage(String, String, String, String, Map)::void"); } /** * * integrate template with data from client program for text replacement */ public String generateEmailContent ( String template, Map map ) throws Exception { Template t = mailTemplateEngine.getConfiguration().getTemplate(template); return FreeMarkerTemplateUtils.processTemplateIntoString(t, map); } /** * * @param subject: subject of the email * @param from: sender of the email * @param to: user to send email to * @param message: message to send */ public void sendSimpleMessage ( String to, String from, String subject, String message ) { logger.debug("BEGIN: sendSimpleMessage(String, String, String, String)::void"); SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo(to); // either passed in the function call or set in the configuration if (from != null) { msg.setFrom(from); } else { msg.setFrom(mailMessage.getFrom()); } if (subject != null) { msg.setSubject(subject); } else { msg.setSubject(mailMessage.getSubject()); } msg.setText(message); mailSender.send(msg); logger.debug("END: sendSimpleMessage(String, String, String, String)::void"); } }