I am writing a program that sends a HTML newsletter to a bunch of people. Currently, the way this is done is to create the html, and replace all images with cid:#### references. This is then sent to users using the Apache Commons Email library.
The code for this follows:
public boolean Mail(HttpServletRequest request)
{
try {
HtmlEmail email = new HtmlEmail();
email.setHostName(SMTPServer);
email.setSmtpPort(25);
email.setFrom(FromAddress);
email.addTo(ToAddress);
email.setSubject(Subject);
email.setHtmlMsg(getMailerHTML(request));
email.setTextMsg("Your email client does not support HTML messages.");
//Get the images
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = null;
ServletContext context = getServletContext();
doc = db.parse(context.getRealPath("issues/" +IssueFileName));
NodeList Images = doc.getElementsByTagName("imageURL");
//Attach header
DataSource fds = new FileDataSource
(context.getRealPath("CMS/images/header-mailer.jpg"));
email.embed(fds,"header","header");
//Attach images
for(int i=0;i<Images.getLength();i++) {
fds = new FileDataSource
(context.getRealPath(Images.item(i).getFirstChild().getNodeValue()));
email.embed(fds,"image" + i,"img" + i +"");
}
email.setDebug(true);
email.send();
return true;
}catch(Exception e){
System.out.println("EXCEPTION (Mail): "+e.getMessage());
return false;
}
}
In lotus notes this email appears correctly, with all images present.
When sent to outlook, the email appears completely blank. Viewing the source shows that the HTML is present, however it does not display, not even as text. Initially the mail came up as a "phishing attempt", though the problem occurs even after being added to the safe list.
My only guess is that it may be because all of my HTML is imported as one long string (getMailerHTML() returns String).