Notification Service: E-mail MessageCourier
12 November 2008At this point, the e-mail version of the MessageCourier is relatively simple. I went ahead and built in provisions for the from, to, cc, bcc, and reply-to addresses, but did not do attachments or anything exotic with the message itself beyond a simple message body. For this first iteration, that’s good enough to demonstrate the concept. We can always get fancy later on. Here’s the complete EmailCourier as it stands today:
package org.restafarian.notify.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.SimpleEmail;
import org.restafarian.notify.beans.MessageAddress;
import org.restafarian.notify.service.MessageCourier;
/**
* <p>This is the e-mail implementation of the MessageCourier interface.</p>
*/
public class EmailCourier implements MessageCourier {
private final Log log = LogFactory.getLog(getClass());
private String smtpHostName = "localhost";
private String defaultFromAddress;
/**
* <p>Delivers the message to specified recipient(s).</p>
*
* @param addresses an array of "address" objects containing the
* implementation-specific addresses required for successful delivery of
* the message
* @param titleSubject an optional subject and/or title of the message to
* be delivered
* @param contentType the mime type of the message body
* @param body the text of the message to be delivered
*/
public void deliverMessage(MessageAddress[] addresses, String titleSubject, String contentType,
String body) {
List fromAddresses = new ArrayList();
List toAddresses = new ArrayList();
List ccAddresses = new ArrayList();
List bcAddresses = new ArrayList();
List replyToAddresses = new ArrayList();
if (addresses != null) {
for (int i=0; i<addresses.length; i++) {
if ("mailFrom".equalsIgnoreCase(addresses[i].getAddressType())) {
if (addresses[i].getAddressValues() != null) {
for (int j=0; j<addresses[i].getAddressValues().length; j++) {
fromAddresses.add(addresses[i].getAddressValues()[j]);
}
}
} else if ("mailTo".equalsIgnoreCase(addresses[i].getAddressType())) {
if (addresses[i].getAddressValues() != null) {
for (int j=0; j<addresses[i].getAddressValues().length; j++) {
toAddresses.add(addresses[i].getAddressValues()[j]);
}
}
} else if ("mailCc".equalsIgnoreCase(addresses[i].getAddressType())) {
if (addresses[i].getAddressValues() != null) {
for (int j=0; j<addresses[i].getAddressValues().length; j++) {
ccAddresses.add(addresses[i].getAddressValues()[j]);
}
}
} else if ("mailBc".equalsIgnoreCase(addresses[i].getAddressType())) {
if (addresses[i].getAddressValues() != null) {
for (int j=0; j<addresses[i].getAddressValues().length; j++) {
bcAddresses.add(addresses[i].getAddressValues()[j]);
}
}
} else if ("mailReplyTo".equalsIgnoreCase(addresses[i].getAddressType())) {
if (addresses[i].getAddressValues() != null) {
for (int j=0; j<addresses[i].getAddressValues().length; j++) {
replyToAddresses.add(addresses[i].getAddressValues()[j]);
}
}
}
}
}
String mailFrom = defaultFromAddress;
if (fromAddresses.size() > 0) {
mailFrom = (String) fromAddresses.get(0);
}
try {
SimpleEmail email = new SimpleEmail();
email.setHostName(smtpHostName);
email.setFrom(mailFrom);
if (toAddresses.size() > 0) {
email.setTo(toAddresses);
}
if (ccAddresses.size() > 0) {
email.setCc(ccAddresses);
}
if (bcAddresses.size() > 0) {
email.setBcc(bcAddresses);
}
if (replyToAddresses.size() > 0) {
Iterator i = replyToAddresses.iterator();
while (i.hasNext()) {
email.addReplyTo((String) i.next());
}
}
email.setSubject(titleSubject);
email.setContent(body, contentType);
email.send();
} catch (Exception e) {
log.error("Exception sending notification", e);
}
}
/**
* @return the smtpHostName
*/
public String getSmtpHostName() {
return smtpHostName;
}
/**
* @param smtpHostName the smtpHostName to set
*/
public void setSmtpHostName(String smtpHostName) {
this.smtpHostName = smtpHostName;
}
/**
* @return the defaultFromAddress
*/
public String getDefaultFromAddress() {
return defaultFromAddress;
}
/**
* @param defaultFromAddress the defaultFromAddress to set
*/
public void setDefaultFromAddress(String defaultFromAddress) {
this.defaultFromAddress = defaultFromAddress;
}
}
This version uses Apache Commons E-mail to do the heavy lifting related to the message delivery, which makes the whole thing pretty simple, all things considered. The courier is the last step in the notification process. Basically, by the time you invoke the courier, you have resolved any variables in the message subject and body (presumably using the SimpleVelocityService, although that should probably be configurable as well), and you are ready to have the message delivered.
Getting to that point will be the job of the Notifier, which we’ll detail out next time (unless we stop and build some kind of JUnit test for the EmailCourier, which we need to do as well!). I guess I’ll just have to wait and see what I’m in the mood for when that tme comes around …
Leave a reply
You must be logged in to post a comment.





