1

ロゴとユーザーの署名の画像を埋め込んだ HTML メールを送信しようとしています。私はApache Commonsメールを使用しています。私は Apache のサイト チュートリアルに従い、ウェブ上で見られるさまざまなアプローチを試しましたが、画像を埋め込むことはできません。これはイントラネット アプリケーションであり、外部からのアクセスをブロックするシングル サインオン システムの背後にあるため、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();
4

2 に答える 2

1

私の答えは本当にJavaに関係していないので、私は答えるつもりはありませんでしたが...

base64 エンコーダーを使用して、メールに画像を埋め込むことができます。 http://www.motobit.com/util/base64-decoder-encoder.asp

ただし、ほとんどのクライアントはエンコードされた画像を表示しないため、これに反対することをお勧めします http://www.campaignmonitor.com/blog/post/1761/embedding-images-in-email/

サーバーでホストされている画像への通常のhtmlリンクを投稿するのが最善の策だと思います。

これがあなたが聞きたかった答えでない場合は申し訳ありません。

于 2012-03-27T15:39:36.873 に答える
0

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
うまくいくはずです。それは私のために行います:)

于 2012-11-28T17:35:08.177 に答える