首页 > 解决方案 > Outlook 无法在 MimeMessage 中显示图像

问题描述

我将消息准备为 MimeMessage 并尝试在不同的邮件客户端中打开它。在所有客户端中,我的消息看起来不错,但在 Outlook 和另一个客户端中,它看起来不对

...
MimeMessage mimeMessage = new MimeMessage(session);

// Text version
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(email.getPlainMessage(), "text/plain");

// HTML version
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(email.getMessage(), "text/html");

//logo
PreencodedMimeBodyPart logo = new PreencodedMimeBodyPart(BASE_64_ENCODING);
logo.setHeader("Content-Type", IMAGE_PNG_CONTENT_TYPE + "; name=\"logo.png\"");
logo.setHeader("Content-ID", "<logo.png@01CFFF81.C72F8000>");
logo.setHeader("Content-Disposition", "inline; filename=\"logo.png\"");
logo.setHeader("Content-Transfer-Encoding", BASE_64_ENCODING);
logo.setContent(ENCODED_LOGO, IMAGE_PNG_CONTENT_TYPE);

// Create the Multipart. Add BodyParts to it.
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(logo);
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);

if (Objects.nonNull(email.getFilesToAttach())) {
    for (String filename : email.getFilesToAttach()) {
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename.substring(filename.lastIndexOf(File.separator) + 1));
        mp.addBodyPart(messageBodyPart);
    }
}

// Set Multipart as the message's content
mimeMessage.setContent(mp);

mimeMessage.setSubject(email.getSubject());
mimeMessage.setFrom(new InternetAddress(emailConfig.getNoReplyEmailAddress()));

if (!emailConfig.getNoReplyEmailAddress().equals(email.getFromAddress())) {
    List<Address> replyTo = Arrays.asList(new InternetAddress(email.getFromAddress()));
    mimeMessage.setReplyTo(replyTo.toArray(new Address[replyTo.size()]));
}
mimeMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email.getToAddress()));
mimeMessage.saveChanges();
...

当我在 Outlook 中发送它时,我看到我的徽标为

破损的标志

但在另一个邮件客户端它看起来不错

标签: javaimageemailoutlookhtml-email

解决方案


推荐阅读