ロゴとユーザーの署名の画像を埋め込んだ 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();