我创建了一个电子邮件发送作业,该图片显示了来自excel表格的接收者电子邮件ID,以及来自html文件(带有嵌入图像)的电子邮件内容,这些内容都放置在本地系统中。 但是当邮件通过作业发送时,图像在MS Outlook中不可见,但在yahoo和gmail中可见作为附件。
电子邮件发送代码:
while (it.hasNext())
{
ReadHTMLContent content = new ReadHTMLContent();
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart messageBodyPart1 = new MimeBodyPart();
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
to = (String) it.next();
b = to.matches(emailCheck);
if (b!= null && b == true) {
MimeMessage message = new MimeMessage(session);
msgContent = fetchMailContent();
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("This is the Subject Line!");
messageBodyPart1.setContent(msgContent, "text/html");
multipart.addBodyPart(messageBodyPart1);
String collect = content.getImageSrc();
String imgStrng = collect;
DataSource fds = new FileDataSource(imgStrng);
messageBodyPart2.setDataHandler(new DataHandler(fds));
messageBodyPart2.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully to " + to);
} else {
System.out.println("Invalid Email ID " + to);
}
}
我从中获取邮件内容的示例HTML具有嵌入式图像,本地存储在系统中:
<html>
<head><title>Sample test mail job</title></head>
<body bgcolor=white>
<table border="0" cellpadding="10">
<tr>
<td>
<h1>Testing EMail crone job</h1>
</td>
</tr>
</table>
<p>Weather is cold today </p>
<tr>
<td>
<img src="D:\\Email POC\\images\\Koala.jpg">
</td>
</tr>
</body>
</html>
答案 0 :(得分:0)
Outlook使用Word作为电子邮件编辑器。您可以在MSDN的以下文章中阅读有关受支持和不受支持的HTML元素,属性和级联样式表属性:
img src =&#34; &#34;
您需要添加对加载到任何Web服务器的图像的引用,或者将图像添加为隐藏附件。因此,结果标记应如下所示:
img src =&#34; cid:attachmentName&#34;
string img = "<br/><p><o:p><img src=\"" + att.FileName
+ "\" width=1 height=1 border=0 /></o:p></p>";
item.HTMLBody = item.HTMLBody.Replace("</body>", img + "</body>");
string PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E";
string HIDDEN_ATTACHMENT = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B";
var pa = att.PropertyAccessor;
if (pa != null)
{
pa.SetProperty(PR_ATTACH_CONTENT_ID, att.FileName);
pa.SetProperty(HIDDEN_ATTACHMENT, false);
}
答案 1 :(得分:0)
msgContent字符串需要使用您指定的Content-Id引用附加的图像,即使用:
<img src="cid:image"/>
如果某个其他程序正在创建msgContent,您可能需要处理html并更改对图像的引用。