【总结】OUTLOOK2016邮件样式和inline图片兼容性等问题解决

Div布局不支持

outlook客户端很多都不支持div布局。应该全部换成table布局。

line-height不生效

解决方案

  • mso-line-height-rule: exactly;

  • 在空的单元格中加入空格HTML转义符: 

例如一个水平带颜色的条:

<td height="3"
          width="100%"
          style="mso-line-height-rule: exactly; line-height: 3px; background-color: #1890ff;"
        >&nbsp;</td>

如果没有加&nbsp;则蓝色高度会无效

inline图片不显示,或者显示为红叉

解决方案
https://support.microsoft.com/en-us/help/2779191/inline-images-may-display-as-a-red-x-in-outlook
对于Microsoft Outlook 2016和2013:
该问题的原因:如果未设置以下注册表值,或者不存在以下注册表值:

HKEY_CURRENT_USERSoftwareMicrosoftOfficex.0OutlookOptionsMail

DWORD Key :Send Pictures With Document

值:1

注意:x.0是占位符,需要根据版本选择正确的值:16.0 = Office 2016, 15.0 = Office 2013

Logo等图片不显示(二)

如果图片或者Logo是Inline的,例如base64编码的图片,在浏览器中是显示的,但是在Outlook 2016等客户端是无法显示的。

<img width="45px" height="45px" src="data:image/png;base64,iVBORSLANKKA...."/>

解决方案:
替换成inline附件:

String imgSrc = item.attr("src");
String dataType = StringUtils.substringBetween(imgSrc, "data:", ";base64,"); // extract data type (dataType = "image/png")
String base64EncodedFileContent = imgSrc.replaceFirst("data:.*;base64,", ""); // remove prefix from fileContent String (base64EncodedFileContent = "iVBORSLANKKA......etc"

MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
filePart.setContent(base64EncodedFileContent, dataType);
filePart.setFileName("attachImg"+ (++i) +"." + dataType.substring(dataType.lastIndexOf('/')));
IdGenerator idGenerator = new AlternativeJdkIdGenerator();
String cid = idGenerator.generateId().toString();
filePart.setContentID("<" + cid + ">");
filePart.setDisposition(MimeBodyPart.INLINE);
item.attr("src", "cid:"+ cid);
mainPart.addBodyPart(filePart);

则邮件中的BASE64编码可以转换成PreencodedMimeBodyPart,作为INLINE的附件,通过Cid引用。

原文地址:https://www.cnblogs.com/slankka/p/13914414.html