【问题标题】:Java mail - attachments && inline imagesJava 邮件 - 附件 && 内嵌图片
【发布时间】:2013-07-18 04:43:48
【问题描述】:

今天早上我已经解决了一个问题: Java Mail, sending multiple attachments not working

这次我有一个稍微复杂一点的问题:我想将附件与图像结合起来。

import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailTest
{

    public static void main(String[] args) throws AddressException, MessagingException, IOException
    {
        String host = "***";
        String from = "***";
        String to = "***";

        // Get system properties
        Properties props = System.getProperties();

        // Setup mail server
        props.put("mail.smtp.host", host);

        // Get session
        Session session = Session.getDefaultInstance(props, null);

        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Hello JavaMail");

        // Handle attachment 1
        MimeBodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.attachFile("c:/Temp/a.txt");

        // Handle attachment 2
        MimeBodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2.attachFile("c:/Temp/b.txt");

        FileDataSource fileDs = new FileDataSource("c:/Temp/gti.jpeg");
        MimeBodyPart imageBodypart = new MimeBodyPart();
        imageBodypart.setDataHandler(new DataHandler(fileDs));
        imageBodypart.setHeader("Content-ID", "<myimg>");
        imageBodypart.setDisposition(MimeBodyPart.INLINE);

        // Handle text
        String body = "<html><body>Elotte<img src=\"cid:myimg\" width=\"600\" height=\"90\" alt=\"myimg\" />Utana</body></html>";

        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setHeader("Content-Type", "text/plain; charset=\"utf-8\"");
        textPart.setContent(body, "text/html; charset=utf-8");

        MimeMultipart multipart = new MimeMultipart("mixed");

        multipart.addBodyPart(textPart);
        multipart.addBodyPart(imageBodypart);
        multipart.addBodyPart(messageBodyPart1);
        multipart.addBodyPart(messageBodyPart2);

        message.setContent(multipart);

        // Send message
        Transport.send(message);
    }
}

当我在 Gmail 中打开电子邮件时,一切都很好:我有两个附件,并且图像显示在邮件内容中(在 img 标记中)。

问题在于 Thunderbird 和 RoundCubic 网络邮件:每个都显示为图像丢失,并在底部显示为附件。

我怎样才能做到这一点?

【问题讨论】:

  • Microsoft Outlook 2010 不再允许使用内联图像!这对你来说是个大问题吗?
  • 好吧,我使用的不是 Outlook,而是 Thunderbird。在我将多部分从“相关”更改为“混合”之前,它正在工作。请。请参阅链接的 stackoverflow 问题。
  • 所以我用这个解决了这个问题:static.springsource.org/spring/docs/1.2.x/reference/mail.html Spring 在后台做这个把戏。但我无法结束这个问题,因为我的代表不够高。

标签: java jakarta-mail


【解决方案1】:

org.apache.commons.mail library 使用 ImageHtmlEmail 也很方便。 (更新:仅包含在 1.3 的快照中)例如:

  HtmlEmail email = new ImageHtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("Test email with inline image");

  // embed the image and get the content id
  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed(url, "Apache logo");

  // set the html message
  email.setHtmlMsg(htmlEmailTemplate, new File("").toURI().toURL(), false);

【讨论】:

    【解决方案2】:

    所以我通过让 javamail 使用 spring 包装器来解决这个问题:

    http://static.springsource.org/spring/docs/1.2.x/reference/mail.html

    我不知道它在后台做了什么魔法,但它确实有效。

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 2017-03-16
      • 2012-03-22
      • 2016-07-30
      • 2015-10-09
      • 1970-01-01
      • 2011-09-10
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多