简体   繁体   中英

sending an email with an embedded Image

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();

I wasnt going to answer because my answer isnt really related to java but...

It is possible to embed an image in an email using a base64 encoder. http://www.motobit.com/util/base64-decoder-encoder.asp

I would suggest againt this though because most clients dont show encoded images http://www.campaignmonitor.com/blog/post/1761/embedding-images-in-email/

I think your best bet would be post a normal html link to an image hosted on a server.

Sorry if this isnt the answer you wanted to hear.

Don't go through the hassle of the XmlHelper. It's probably what's making it not work.
Just change the img tag in the email like this src="CIDSIGNATURE" and then do this:

HtmlEmail htmlMail = new HtmlEmail(); 
initMail(htmlMail);//set commons parameters (host,port,...
//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");
htmlCorpoMessaggio = htmlCorpoMessaggio.replace("CIDSIGNATURE", "cid:" + cid);
htmlMail.setHtmlMsg(htmlCorpoMessaggio);
htmlMail.send();

Notice I removed the htmlMail.setContent at the beginning.
Should work fine. It does for me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM