【问题标题】:sending an email with an embedded Image发送带有嵌入图像的电子邮件
【发布时间】:2012-04-07 23:50:50
【问题描述】:

我正在尝试发送嵌入徽标和用户签名图像的 html 电子邮件。我正在使用 apache commons 邮件。我遵循了 Apache 的网站教程并尝试了在网上找到的不同方法,但我无法嵌入任何图像。 我想说我不能使用 urls 来获取嵌入的图像,因为这是一个 Intranet 应用程序,而且无论如何,它位于一个单一登录系统的后面,这将阻止来自外部的任何访问。 此外,这不是真正的 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();

【问题讨论】:

    标签: java email html-email apache-commons-email


    【解决方案1】:

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

    可以使用 base64 编码器在电子邮件中嵌入图像。 http://www.motobit.com/util/base64-decoder-encoder.asp

    我建议再次这样做,因为大多数客户端不显示编码图像 http://www.campaignmonitor.com/blog/post/1761/embedding-images-in-email/

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

    对不起,如果这不是您想听到的答案。

    【讨论】:

    • @Sam Warren 好吧,谢谢你,至少有人回答了:),我学到了一些新东西。 Unlickly 无法从 Web 访问 Web 应用程序,只能从 Intranet 内部访问,并且无论如何所有资源都位于 sso 系统后面,该系统会阻止任何未经身份验证的请求
    【解决方案2】:

    不要经历 XmlHelper 的麻烦。这可能是导致它不起作用的原因。
    只需像这样src="CIDSIGNATURE" 更改电子邮件中的 img 标签,然后执行以下操作:

    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。
    应该可以正常工作。它对我有用:)

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 1970-01-01
      • 2015-07-29
      • 2014-01-25
      • 1970-01-01
      • 2011-04-16
      • 2012-12-08
      • 1970-01-01
      相关资源
      最近更新 更多