{
String errorMessage = "";
try {
InitialContext ic = CVUtility.getInitialContext();
MailLocalHome home = (MailLocalHome)ic.lookup("local/Mail");
MailLocal remote = home.create();
remote.setDataSource(dataSource);
messageVO.setEmailAccountID(accountID);
MailAccountVO accountVO = remote.getMailAccountVO(accountID);
if (accountVO == null) {
accountVO = new MailAccountVO();
}
if (accountVO.getAccountID() != -1) {
InternetAddress fromAddress = new InternetAddress(accountVO.getEmailAddress(), accountVO.getAccountName());
messageVO.setFromAddress(fromAddress.toString());
messageVO.setReplyTo(fromAddress.toString());
messageVO.setSubject(subject);
messageVO.setHeaders("");
messageVO.setContentType(MailMessageVO.HTML_TEXT_TYPE);
messageVO.setReceivedDate(new java.util.Date());
if (content != null && content.size() != 0) {
// content is an arrayList of strings which are the customized messages
for (int i = 0; i < content.size(); i++) {
String bodyContent = (String)content.get(i);
messageVO.setBody(bodyContent);
// to is an arraylist of corresponding addresses
String toAddress = (String)to.get(i);
if (CVUtility.isEmailAddressValid(toAddress)) {
ArrayList toList = new ArrayList();
toList.add(toAddress);
messageVO.setToList(toList);
try {
boolean messageSent = remote.sendMessage(accountVO, messageVO);
if (messageSent) {
logger.debug("["+dataSource+"]Successfully Sent Mail Merge to: "+toAddress);
} else {
logger.error("["+dataSource+"]Mail Merge Failed sending message to: "+messageVO.getToList()+", from account: "+accountVO.getAccountID());
}