Java 邮件 - 附件和内联图像

标签 java jakarta-mail

今天早上我已经解决了一个问题: 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 webmail 上:每个显示的图像都丢失了,并将其作为附件显示在底部。

我怎样才能让它工作?

最佳答案

使用 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);

关于Java 邮件 - 附件和内联图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8973026/

相关文章:

java - oracle ordim 为图像添加水印

java - fragment 选项菜单 - 在 fragment 堆栈中创建多个实例

Java 程序和 YouTube API

java - 计算 Java 类校验和的最佳方法是什么?

java - 如何在应用程序内使用用户的电子邮件 ID 发送电子邮件(无 Intent )

java - 我无法将我的用户名和密码从一个 jframe 转移到另一个 jframe

java - Google 数据存储中的重复条目

java - 在 GAE 的本地环境中发送电子邮件

java - 使用 spring boot 和带有代理的 JavaMailSender 发送邮件

java - 无法使用 JavaMail POP 获取 Gmail 收件箱