Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.dao; import com.sfs.ConvertHtmlToText; import com.sfs.beans.EmailMessageBean; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.StringTokenizer; import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.UrlResource; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; /** * The Class EmailMessageDAOImpl. */ public class EmailMessageDAOImpl extends BaseDAOImpl implements EmailMessageDAO { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(EmailMessageDAOImpl.class); /** The debug mode. */ private boolean debugMode = false; /** The java mail sender. */ @Resource private JavaMailSender javaMailSender; /** * Sets the debug mode. * * @param debugModeVal boolean on or off */ public final void setDebugMode(final boolean debugModeVal) { this.debugMode = debugModeVal; } /** * Send an email message using the configured Spring sender. On success * record the sent message in the datastore for reporting purposes * * @param emailMessage the email message * * @throws SFSDaoException the SFS dao exception */ public final void send(final EmailMessageBean emailMessage) throws SFSDaoException { // Check to see whether the required fields are set (to, from, message) if (StringUtils.isBlank(emailMessage.getTo())) { throw new SFSDaoException("Error recording email: Recipient " + "address required"); } if (StringUtils.isBlank(emailMessage.getFrom())) { throw new SFSDaoException("Error recording email: Email requires " + "a return address"); } if (StringUtils.isBlank(emailMessage.getMessage())) { throw new SFSDaoException("Error recording email: No email " + "message specified"); } if (javaMailSender == null) { throw new SFSDaoException("The EmailMessageDAO has not " + "been configured"); } // Prepare the email message MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = null; if (emailMessage.getHtmlMessage()) { try { helper = new MimeMessageHelper(message, true, "UTF-8"); } catch (MessagingException me) { throw new SFSDaoException("Error preparing email for sending: " + me.getMessage()); } } else { helper = new MimeMessageHelper(message); } if (helper == null) { throw new SFSDaoException("The MimeMessageHelper cannot be null"); } try { if (StringUtils.isNotBlank(emailMessage.getTo())) { StringTokenizer tk = new StringTokenizer(emailMessage.getTo(), ","); while (tk.hasMoreTokens()) { String address = tk.nextToken(); helper.addTo(address); } } if (StringUtils.isNotBlank(emailMessage.getCC())) { StringTokenizer tk = new StringTokenizer(emailMessage.getCC(), ","); while (tk.hasMoreTokens()) { String address = tk.nextToken(); helper.addCc(address); } } if (StringUtils.isNotBlank(emailMessage.getBCC())) { StringTokenizer tk = new StringTokenizer(emailMessage.getBCC(), ","); while (tk.hasMoreTokens()) { String address = tk.nextToken(); helper.addBcc(address); } } if (StringUtils.isNotBlank(emailMessage.getFrom())) { if (StringUtils.isNotBlank(emailMessage.getFromName())) { try { helper.setFrom(emailMessage.getFrom(), emailMessage.getFromName()); } catch (UnsupportedEncodingException uee) { dataLogger.error("Error setting email from", uee); } } else { helper.setFrom(emailMessage.getFrom()); } } helper.setSubject(emailMessage.getSubject()); helper.setPriority(emailMessage.getPriority()); if (emailMessage.getHtmlMessage()) { final String htmlText = emailMessage.getMessage(); String plainText = htmlText; try { ConvertHtmlToText htmlToText = new ConvertHtmlToText(); plainText = htmlToText.convert(htmlText); } catch (Exception e) { dataLogger.error("Error converting HTML to plain text: " + e.getMessage()); } helper.setText(plainText, htmlText); } else { helper.setText(emailMessage.getMessage()); } helper.setSentDate(emailMessage.getSentDate()); } catch (MessagingException me) { throw new SFSDaoException("Error preparing email for sending: " + me.getMessage()); } // Append any attachments (if an HTML email) if (emailMessage.getHtmlMessage()) { for (String id : emailMessage.getAttachments().keySet()) { final Object reference = emailMessage.getAttachments().get(id); if (reference instanceof File) { try { FileSystemResource res = new FileSystemResource((File) reference); helper.addInline(id, res); } catch (MessagingException me) { dataLogger.error("Error appending File attachment: " + me.getMessage()); } } if (reference instanceof URL) { try { UrlResource res = new UrlResource((URL) reference); helper.addInline(id, res); } catch (MessagingException me) { dataLogger.error("Error appending URL attachment: " + me.getMessage()); } } } } // If not in debug mode send the email if (!debugMode) { deliver(emailMessage, message); } } /** * Deliver the email message. * * @param emailMessage the email message * @param message the message * * @throws SFSDaoException the SFS dao exception */ private void deliver(final EmailMessageBean emailMessage, final MimeMessage message) throws SFSDaoException { // Send the email message try { javaMailSender.send(message); } catch (MailException me) { dataLogger.error("Error sending email: " + me.getMessage()); throw new SFSDaoException("Error sending email: " + me.getMessage()); } } }