JavaMail MimeMultiPart不发送整个内容

时间:2013-11-19 09:10:49

标签: java javamail multipart mime-message

我已经暂时遇到一个奇怪的问题了,我的代码中没有看到任何错误。我正在尝试发送带有java的电子邮件,我将图像附加到邮件中。首先,我使用纯文本消息,然后通过附加它并使用html显示它来显示图像。

我认为这应该是这样的:

+-----------------------------------------------+ 
| multipart/related                             | 
| +---------------------------+  +------------+ | 
| |multipart/alternative      |  | image/gif  | | 
| | +-----------+ +---------+ |  |            | | 
| | |text/plain | |text/html| |  |            | | 
| | +-----------+ +---------+ |  |            | | 
| +---------------------------+  +------------+ | 
+-----------------------------------------------+

这是我的代码:

public void sendEmail(Email email) throws MessagingException {
    String[] recipients = email.getRecipients();
    // Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    //props.put("mail.smtps.auth", "true");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    MimeMessage msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(email.getFrom());
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(email.getSubject());

    BodyPart mbp1 = new MimeBodyPart();
    mbp1.setContent(email.getMessage(), "text/plain");

    BodyPart mbp2 = new MimeBodyPart();
    mbp2.setContent("<html><body><br/><br/><br/><img src=\"cid:image"><br/>"
            + "Support<br/>"
            + "Carl Olofsson<br/>"
            + "Management Unit<br/>"
            + "----<br/>"
            + "----<br/>"
            + "Sweden<br/>"
            + "www.-----.com<br/></body></html>", "text/html");

    Multipart contentMultipart = new MimeMultipart("alternative");
    contentMultipart.addBodyPart(mbp1);
    contentMultipart.addBodyPart(mbp2);

    BodyPart contentBodyPart = new MimeBodyPart();
    contentBodyPart.setContent(contentMultipart);

    BodyPart mbp3 = new MimeBodyPart();
    DataSource ds = new FileDataSource("C:/Users/000/Desktop/image.jpg");
    mbp3.setDataHandler(new DataHandler(ds));
    mbp3.setHeader("Content-ID","image");

    Multipart entireMultipart = new MimeMultipart("related");
    entireMultipart.addBodyPart(contentBodyPart);
    entireMultipart.addBodyPart(mbp3);

    // attaching the multi-part to the message
    msg.setContent(entireMultipart);

    // set the message content here
    Transport t = session.getTransport("smtp");
    try {
        t.connect(host, username, password);
        t.sendMessage(msg, msg.getAllRecipients());
    } catch(Throwable throwable){
        LOG.error(throwable.getMessage());
    }finally {
        t.close();
    }
}

}

邮件已发送,但它显示的唯一内容是图片的“页脚”,而不是邮件!

你能在代码中看到错误吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

图像与文本部分并不真正相关。尝试颠倒多部分:

multipart/alternative
  text/plain
  multipart/related
    text/html
    image/gif