使用JavaMail在电子邮件中嵌入图像 [英] Inline images in email using JavaMail

查看:1782
本文介绍了使用JavaMail在电子邮件中嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javamail发送带有内联图片的电子邮件。

I want to send an email with an inline image using javamail.

我正在做这样的事情。

MimeMultipart content = new MimeMultipart("related");

BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(message, "text/html; charset=ISO-8859-1");
content.addBodyPart(bodyPart);

bodyPart = new MimeBodyPart();
DataSource ds = new ByteArrayDataSource(image, "image/jpeg");
bodyPart.setDataHandler(new DataHandler(ds));
bodyPart.setHeader("Content-Type", "image/jpeg; name=image.jpg");
bodyPart.setHeader("Content-ID", "<image>");
bodyPart.setHeader("Content-Disposition", "inline");
content.addBodyPart(bodyPart);

msg.setContent(content);

我也试过

    bodyPart.setHeader("inline; filename=image.jpg");

    bodyPart.setDisposition("inline");

但无论如何,图片都是作为附件发送的,而Content-Dispostion正在变成附件。

but no matter what, the image is being sent as an attachment and the Content-Dispostion is turning into "attachment".

如何使用javamail在电子邮件中内联发送图像?

How do I send an image inline in the email using javamail?

推荐答案

你的问题



据我所知,它看起来像你创建消息的方式,一切都是正确的!您使用正确的MIME类型和所有内容。

Your problem

As far as I can see, it looks like the way you create the message and everything is mostly right! You use the right MIME types and everything.

我不确定您为什么使用DataSource和DataHandler,并在图像上有ContentID,但您需要完成我有问题能够解决更多问题。特别是,以下行:

I am not sure why you use a DataSource and DataHandler, and have a ContentID on the image, but you need to complete your question for me to be able to troubleshoot more. Especially, the following line:

bodyPart.setContent(message, "text/html; charset=ISO-8859-1");

什么是消息?它是否包含< img src =cid:image/>

您是否尝试生成ContentID with String cid = ContentIdGenerator.getContentId(); 而不是使用 image

Did you try to generate the ContentID with String cid = ContentIdGenerator.getContentId(); instead of using image

此博客文章教我如何使用正确的消息类型,附上我的图像并参考HTML正文中的附件:如何使用Java发送带嵌入式图像的电子邮件

This blog article taught me how to use the right message type, attach my image and refer to the attachment from the HTML body: How to Send Email with Embedded Images Using Java

您必须使用 MimeMultipart 类。使用字符串related作为构造函数的参数非常重要,告诉JavaMail您的部件一起工作。 / p>

You have to create your content using the MimeMultipart class. It is important to use the string "related" as a parameter to the constructor, to tell JavaMail that your parts are "working together".

MimeMultipart content = new MimeMultipart("related");



内容标识符



您需要生成ContentID,它是一个字符串,用于标识您附加到电子邮件中的图像,并从电子邮件正文中引用它。

Content identifier

You need to generate a ContentID, it is a string used to identify the image you attached to your email and refer to it from the email body.

String cid = ContentIdGenerator.getContentId();

注意:此 ContentIdGenerator 类是假设的。您可以创建一个或内联创建内容ID。在我的例子中,我使用一个简单的方法:

Note: This ContentIdGenerator class is hypothetical. You could create one or inline the creation of content IDs. In my case, I use a simple method:

import java.util.UUID;

// ...

String generateContentId(String prefix) {
     return String.format("%s-%s", prefix, UUID.randomUUID());
}



HTML正文



HTML代码是 <$ c $的一部分c> MimeMultipart 内容。使用 MimeBodyPart 等级。设置该部分的文本时,不要忘记指定 encoding html

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(""
  + "<html>"
  + " <body>"
  + "  <p>Here is my image:</p>"
  + "  <img src=\"cid:" + cid + "\" />"
  + " </body>"
  + "</html>" 
  ,"US-ASCII", "html");
content.addBodyPart(htmlPart);

请注意,作为图片的来源,我们使用 cid:和生成的ContentID。

Note that as a source of the image, we use cid: and the generated ContentID.

我们可以创建另一个 MimeBodyPart 用于附加图像。

We can create another MimeBodyPart for the attachment of the image.

MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("resources/teapot.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);

请注意,我们在< > 并将其设置为图像的ContentID。我们还将处置设置为 INLINE ,以表示此图像将显示在电子邮件中,而不是作为附件。

Note that we use the same ContentID between < and > and set it as the image's ContentID. We also set the disposition to INLINE to signal that this image is meant to be displayed in the email, not as an attachment.

就是这样!如果您在正确的会话上创建SMTP消息并使用该内容,则您的电子邮件将包含嵌入的图像!例如:

That's it! If you create an SMTP message on the right session and use that content, your email will contain an embedded image! For instance:

SMTPMessage m = new SMTPMessage(session);
m.setContent(content);
m.setSubject("Mail with embedded image");
m.setRecipient(RecipientType.TO, new InternetAddress("your@email.com"));
Transport.send(m)

请告诉我这是否适合您! ;)

Let me know if that works for you! ;)

这篇关于使用JavaMail在电子邮件中嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆