Java 发送带有嵌入图像的电子邮件

Java 发送带有嵌入图像的电子邮件,java,email,html-email,apache-commons-email,Java,Email,Html Email,Apache Commons Email,我正在尝试发送html电子邮件,其中嵌入了一个徽标和用户签名的图像。我正在使用apache commons邮件。我遵循了Apache的站点教程,尝试了在web上找到的不同方法,但我无法嵌入任何图像。 我想说的是,我不能使用URL来获取嵌入的映像,因为这是一个内部网应用程序,而且,无论如何,它位于一个单一登录系统的后面,该系统将阻止来自外部的任何访问。 此外,这不是真正的html,而是应用程序用作模板的xml。 下面我添加了xml-html(注意文本显示正确,只是嵌入了图像有问题),以及我用来嵌入

我正在尝试发送html电子邮件,其中嵌入了一个徽标和用户签名的图像。我正在使用apache commons邮件。我遵循了Apache的站点教程,尝试了在web上找到的不同方法,但我无法嵌入任何图像。 我想说的是,我不能使用URL来获取嵌入的映像,因为这是一个内部网应用程序,而且,无论如何,它位于一个单一登录系统的后面,该系统将阻止来自外部的任何访问。 此外,这不是真正的html,而是应用程序用作模板的xml。 下面我添加了xml-html(注意文本显示正确,只是嵌入了图像有问题),以及我用来嵌入图像的代码,有人能指出我所做的任何错误或建议我的问题的解决方案吗

生成的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>
...


... ...
我发送邮件的代码:

            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();
HtmlEmail htmlMail=新HtmlEmail();
initMail(htmlMail)//设置公用参数(主机、端口等),。。。
setContent(htmlCorpoMessaggio,“text/html”);
//我正在尝试从我的应用程序资源中检索原始字节数组
InputStream=this.getClass().getResourceAsStream(
字符串格式(“%s%s”,
Configurator.getString(“Template.resources”),
getString(“Template.firma”);
byte[]image=IOUtils.toByteArray(is);
//无法发送url我正在尝试将图像真正嵌入邮件消息中
数据源ds=新的ByteArrayDataSource(图像,“图像/png”);
字符串cid=htmlMail.embed(ds,“签名”);
//我需要将src=“an app path”替换为cid
Document doc=XmlHelper.loadXMLFromString(htmlcorpomesaggio);
NodeList NodeList=doc.getElementsByTagName(“img”);
节点currentNode=null;
for(int i=0;i
我不打算回答,因为我的答案实际上与java无关,但

可以使用base64编码器在电子邮件中嵌入图像。

不过我还是建议不要这样做,因为大多数客户都不显示编码图像

我认为你最好的选择是将一个普通的html链接发布到服务器上托管的图像上


很抱歉,如果这不是您想听的答案。

我不打算回答,因为我的答案与java无关,但是

可以使用base64编码器在电子邮件中嵌入图像。

不过我还是建议不要这样做,因为大多数客户都不显示编码图像

我认为你最好的选择是将一个普通的html链接发布到服务器上托管的图像上


如果这不是您想要听到的答案,那么很抱歉。

不要经历XmlHelper的麻烦。这可能是导致它无法工作的原因。
只需更改电子邮件中的img标记,如下所示
src=“CIDSIGNATURE”
,然后执行以下操作:

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();
注意,我在开头删除了
htmlMail.setContent


应该可以。对我来说是这样的:)

不要经历XmlHelper的麻烦。这可能是它不起作用的原因。
只需更改电子邮件中的img标记,如下所示
src=“CIDSIGNATURE”
,然后执行以下操作:

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();
注意,我在开头删除了
htmlMail.setContent


应该行得通。对我来说是这样:)

@Sam Warren不管怎样,谢谢你,至少有人回答了:),我学到了一些新东西。毫无疑问,web应用程序无法从web访问,只能从内部网访问,而且所有资源都位于sso系统后面,该系统将阻止任何未经验证的请求@Sam Warren嗯,无论如何,谢谢你,至少有人回答:),我学到了一些新东西。毫无疑问,web应用程序无法从web访问,只能从内部网访问,而且所有资源都位于sso系统后面,该系统将阻止任何未经验证的请求