在Java Mail创建的消息中内联显示的图像在Apple Mail中显示两次 - 但不是Outlook?

时间:2013-09-01 02:31:42

标签: javamail

在Java Mail创建的邮件中内联显示的图像在Apple Mail中显示两次 - 但不显示Outlook?

EmailMaster会发送测试电子邮件,其中包括: 1)与电子邮件一起发送的图像,以便在打开消息时在标题中内嵌显示 2)显示消息后必须从远程URL下载的图像的链接。

目标是在打开消息(owl)时完全呈现消息。在看到完整信息之前,不要求读者下载它。

当在Outlook等客户端中打开邮件时,以下代码可以正常运行。 - 猫头鹰图像在打开时显示在行中,和 - 必须下载下划线。

当AppleMail打开消息时,Owl会显示两次。屏幕显示: - 猫头鹰作为独立图像, - 然后在标题中正确显示猫头鹰的消息。

我无法发布屏幕截图,但是我在屏幕截图显示了AppleMail屏幕 http://america-3.org/images/shot.jpg

有人能指出我的问题原因吗?还是一个解决方案?

HTML代码添加为一个MimeBodyPart。

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="cid:fonts" rel="stylesheet" type="text/css" />
    <style type="text/css"> 
    <!--
      .text-blue-12 {font-family: Verdana, Arial, Helvetica, sans-serif;
                     text-decoration: none; font-weight: normal; 
                     font-size:12pt; color: #0F0F0F}
       body { margin: 0; padding: 0; background-color: #1A4576; width: 480px;} 
       tr {width:100%;  background-color: white;}
     --> 
     </style>     
  </head>
  <body>
      <table width='480px' cellspacing='0' cellpadding='6' class='text-blue-12' align="center">
        <tr><td style='text-align: center;'>
            Hello! <img src='cid:owl'>
        </td></tr>
        <tr><td style='text-align: center;'>
          <img align="center" alt="Shadow" class="kmImage"
               src="https://d3k81ch9hvuctc.cloudfront.net/assets/email/bottom_shadow_444.png"
               width="600" style="border: 0; height: auto; line-height: 100%;
               outline:none; text-decoration: none; max-width: 100%; padding-bottom: 0; 
               display: inline; vertical-align: bottom" />
        </td></tr>
        <tr><td>
          This is test # 1005.  The Table should be a 480px wide and centered.
          If not there is a problem.
        </td></tr>
      <tr><td>
        Yours truly,
        GLB
      </td></tr>
    </table>
  </body>
</html>

Java Class I的提取用于测试JavaMail代码

public class EmailMaster {
  /* JavaMail attempts to use IPv6 to  connect. Windows IPv4.  
   * The error message will be "Network Unreachable: Connect.
   * To fix it, I ran this code from the command window 
   * setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true
   * I tried to set _JAVA_OPTIONS as an environment variable but that didn't
   * work.
   */

/* the constructor instantiates EmailMaster. 
 * It parses a ".properties" text file that provides the everything 
 * needed to define a message:
 * -- send from, reply to, subject etc. Strings
 * -- local paths to one or more files:
 *      .. a text file containing the to addresses.
 *      .. the HTML file that holds the message body,
 *      .. one of more images to be displayed inline
 *         in the HTML message body.  I used one.
 *      .. one or more files to be attached to the msg. I used one.
 */ 

/* then it calls sendEmail() to read through the list of addresses 
 * and sends an individual email to each address*/

sendEmail () {
  /*this section of code is done once. */
  Session session = Session.getInstance(this.sessionParameters);
  SMTPTransport tr = new SMTPTransport(session, new URLName (host));
  tr.connect(...); // this is successful

  /* the method loops through this section for every to address provided*/
  Message msg = new MimeMessage(session);
  msg.setReplyTo(this.replyToAddress);// read from properties file
  msg.setSentDate(new java.util.Date());// read from properties file
  msg.setSubject(this.subject);// read from properties file
  msg.setFrom(this.fromAddress);// read from properties file
  msg.setRecipient(RecipientType.TO, singleToAddress);// read from properties file

  MimeMultipart mmp = new MimeMultipart();

  /* add a MimeBodyPart for each image be displayed inline the HTML text */
  for (MimeBodyPart part : this.imagesBodyParts.values()) {
    mmp.addBodyPart(part);
    /* add a MimeBodyPart for each HTML page in the message. */
    for (MimeBodyPart part : this.attachmentBodyParts.values()) {
      mmp.addBodyPart(part);
    }
    mmp.addBodyPart(this.msgPart);
    msg.setContent(mmp);
    msg.saveChanges();
    tr.sendMessage(msg, msg.getAllRecipients());
  }
}

1 个答案:

答案 0 :(得分:0)

对邮件阅读器如何显示邮件的控制程度有限。最后,您可能无法完成您尝试对所有邮件阅读器执行的操作。也就是说,您发布的代码存在一些问题......

你有一个双嵌套“for”循环,首先添加一个图像正文部分,然后添加一堆附件(html?)正文部分,然后发送消息,然后将更多的两个添加到同一个消息,然后再次发送消息,依此类推。当然这不是你想要的。

如果您希望消息包含单个html部件和html部件引用的某些图像部件,则需要创建多部件/相关消息,并且图像部件需要Content-ID标头。 This JavaMail FAQ entry会指出正确的方向。

最后,您永远不应该直接调用SMTPTransport的构造函数。相反,您应该使用Session.getTransport方法。