i'm trying to send html emails that embed a logo and an image of the user's signature. I'm using apache commons mail. I've followed Apache's site tutorial and tried different approaches found around on the web, yet i'm not able to embed any image . I wish to remark that i can't use urls to get the embedded image , since this is an intranet application and , anyway , its behind a single sign on system which will block any access from outside. Moreover that's not truly html but rather an xml the application uses as a template. Below i've added the xml - html (Note text gets correctly displayed , just embedded images ahve problems ) , and the code i use to embed the image, can anyone point out any mistake i'm doing or suggest a solution to my problem please ?
the resulting html/xml :
<?xml version="1.0" encoding="UTF-8"?><div style="margin-top: 20px; font-size: small;">
<br/>
<div class="auto-style1">
<div style="text-align: left;">
...
<div class="MsoNormal" style="text-align: right; padding-right: 100px; font-family: arial black,sans-serif;">
<img id="signature" src="cid:jrvoirylpp"/>
</div>
...
my code to send the mail :
HtmlEmail htmlMail = new HtmlEmail();
initMail(htmlMail);//set commons parameters (host,port,...
htmlMail.setContent(htmlCorpoMessaggio, "text/html");
//i'm trying to retrieve the raw byte array from my app resources
InputStream is = this.getClass().getResourceAsStream(
String.format("%s%s",
Configurator.getString("Template.resources"),
Configurator.getString("Template.firma")));
byte[] image = IOUtils.toByteArray(is);
//can't send an url i'm trying to truly embed the image inside the mail message
DataSource ds = new ByteArrayDataSource(image, "image/png");
String cid = htmlMail.embed(ds, "signature");
//i need to replace the src="an app path" to cid
Document doc = XmlHelper.loadXMLFromString(htmlCorpoMessaggio);
NodeList nodeList = doc.getElementsByTagName("img");
Node currentNode = null;
for(int i = 0; i < nodeList.getLength(); i++)
{
currentNode = nodeList.item(i);
}
NamedNodeMap nodiAttributo = currentNode.getAttributes();
for(int i= 0 ; i < nodiAttributo.getLength() ; i++ )
{
Node n = nodiAttributo.item(i);
if(n.getNodeName().equals("src"))
n.setNodeValue("cid:" + cid);
}
htmlCorpoMessaggio = XmlHelper.getStringFromDocument(doc);
for(MailAttachment allegato : allegati)
{
//la stringa vuota rappresenta la descrizione dell'allegato
htmlMail.attach(allegato.getDataSource(),
allegato.getFilename(),"",EmailAttachment.ATTACHMENT);
}
htmlMail.send();