Java邮件-附件和&嵌入式图像

dbalakirev:

我今天早上已经解决了一个问题:Java Mail,发送多个附件不起作用

这次,我遇到了一个稍微复杂的问题:我想将附件和图像结合在一起。

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网络邮件上:每一个都缺少图像,并以附件的形式显示在底部。

我该如何工作?

s106mo:

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

  HtmlEmail email = new ImageHtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("[email protected]", "John Doe");
  email.setFrom("[email protected]", "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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

创建带有嵌入式图像和PDF附件的HTML邮件

curl:发送带有嵌入式图像和附件的html电子邮件

Python:多部分html电子邮件与嵌入式图像和附件一起发送

Java邮件轮询从邮件中读取嵌入式或嵌入式图像(笑脸)

如何阻止电子邮件中的嵌入式图像通过GMail以附件形式显示?

将图像比例限制为C#构造电子邮件中的嵌入式附件

使用Django在HTML电子邮件模板中将图像作为嵌入式附件发送

对Thunderbird中的嵌入式电子邮件图像使用Content-ID和cid

以html格式从电子邮件中读取嵌入式嵌入式图像

PowerShell 中电子邮件的嵌入式图像

在Google App Engine(Java)中发送带有嵌入式图像的电子邮件

如何发送带有嵌入式附件的电子邮件

如何使用Laravel 5.8在电子邮件中嵌入嵌入式图像

嵌入式Java SE和Java ME

使用JavaMail发送带有嵌入式图像的HTML电子邮件-图像加载缓慢?

Python HTML-嵌入式电子邮件图像为空

Python-使用嵌入式图像将邮件发送到GMAIL

如何使用Mailkit读取电子邮件正文中的嵌入式图像

使用python脚本发送带有嵌入式图像的html电子邮件

发送包含嵌入式图像的多部分html电子邮件

Ubuntu的。发送带有嵌入式图像的电子邮件

使用 PHP imap 获取电子邮件会弄乱嵌入式图像

如何使用MailApp在电子邮件中包含嵌入式图像

如何在电子邮件消息中的嵌入式图像中添加“名称”标头

如何发送带有base64嵌入式图像的电子邮件

我可以在收到的电子邮件中找到有关嵌入式图像的信息?

使用Microsoft Graph或Outlook REST API在电子邮件正文中渲染嵌入式图像

Exchange服务器阻止带有嵌入式图像的电子邮件

无法在Google Apps脚本中向电子邮件添加嵌入式图像