【问题标题】:Embedding images into html email with java mail使用 java 邮件将图像嵌入到 html 电子邮件中
【发布时间】:2011-07-12 18:02:23
【问题描述】:

我正在使用 javamail 发送 html 和图像,但由于某种原因,我没有将图像视为 html 的一部分,我仅将它们视为附件。我不知道为什么会这样。这是我的一个用户收到电子邮件时的样子:

我还想提一下 html 的样子:

private String generateActivationLinkTemplate() {
    String htmlText = "";
htmlText ="<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">  <tr>    <td><img src=\"cid:logoimg\"/></td>  </tr>  <tr>    <td height=\"220\"> <p>Thanks for Joining Site.com</p>      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>    <p>Username:<br />      Password: </p>    <p>To confirm your email click <a href=\"#\">here</a>.</p></td>  </tr>  <tr>    <td height=\"50\" align=\"center\" valign=\"middle\" bgcolor=\"#CCCCCC\">www.site.com | contact@site.com | +38200 123 456</td>  </tr></table>";}

我需要 html、body 和 head 标签吗?

这是java实现的样子:

@Stateless(name = "ejbs/EmailServiceEJB")
public class EmailServiceEJB implements IEmailServiceEJB {

@Resource(name = "mail/myMailSession")
private Session mailSession;

public void sendAccountActivationLinkToBuyer(String destinationEmail,
        String name) {

    // Destination of the email
    String to = destinationEmail;
    String from = "dontreply2thismessage@gmail.com";

    try {
        Message message = new MimeMessage(mailSession);
        // From: is our service
        message.setFrom(new InternetAddress(from));
        // To: destination given
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject("Uspijesna registracija");
        // How to found at http://www.rgagnon.com/javadetails/java-0321.html
        message.setContent(generateActivationLinkTemplate(), "text/html");

        Date timeStamp = new Date();
        message.setSentDate(timeStamp);

        // Prepare a multipart HTML
        Multipart multipart = new MimeMultipart();
        // Prepare the HTML
        BodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(generateActivationLinkTemplate(), "text/html");

        // PREPARE THE IMAGE
        BodyPart imgPart = new MimeBodyPart();

        String fileName = "logoemailtemplate.png";

        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        if (classLoader == null) {
            classLoader = this.getClass().getClassLoader();
            if (classLoader == null) {
                System.out.println("IT IS NULL AGAIN!!!!");
            }
        }

        DataSource ds = new URLDataSource(classLoader.getResource(fileName));

        imgPart.setDataHandler(new DataHandler(ds));
        imgPart.setHeader("Content-ID", "logoimg");

        multipart.addBodyPart(imgPart);
        multipart.addBodyPart(htmlPart);            

        // Set the message content!
        message.setContent(multipart);

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

我认为 java 部分对我来说看起来不错,但我怀疑只是 html 标记,我有什么问题吗?我认为 img 标签无法正常工作,并且图像没有出现在电子邮件中(注意它只显示为附件):

<img src=\"cid:logoimg\"/>

【问题讨论】:

  • 你能发布生成的 MIME 代码,或者至少是标题可见但内容被修剪的 MIME 代码吗?
  • 能否请您告诉我如何查找或生成 MIME 代码,以便我发布。我有点困惑。
  • 我唯一改变的是这个 imgPart.setHeader("Content-ID", "");现在完美运行。

标签: java html jakarta-ee ejb-3.0 jakarta-mail


【解决方案1】:

错误是因为&lt;img src=\"cid:logoimg\"/&gt;

应该有:imgPart.setHeader("Content-ID", "&lt;logoimg&gt;");

不是:imgPart.setHeader("Content-ID", "logoimg");

即:你需要''

【讨论】:

  • 谢谢!这有帮助。我也在尝试“logoimg”而不是“”
【解决方案2】:

您是否检查过 content-type 是否正确以及图像 content-disposition 是否设置为 inline?

此外,Content-ID 需要是全球唯一的,您不能只说“logoimg”。试试logimg-randomnumbers-dontreply2thismessage@gmail.com。不过,这可能不是你的问题。

【讨论】:

  • 我使用了这两种方法 imgPart.setDisposition(BodyPart.INLINE);还有 htmlPart.setDisposition(BodyPart.INLINE);但结果是一样的。我不明白您对内容 ID 所说的内容,您能解释一下我应该如何处理它吗?
  • 您将 content-id 设置为“logoimg”。内容 ID 应该是一个在世界任何地方都不会重复的唯一标识符,并且它必须包含一个 @ 符号。为什么,我不知道。您需要向我们展示生成的 MIME。电子邮件产生什么 MIME?
  • 好吧,我对 html 标签属性有点缺乏经验。你的意思是这样的:?我也不明白如何找到电子邮件产生的 MIME,我在哪里可以找到?
  • 我发现了这个:multipart/mixed; boundary="----=_Part_16_27478153.1299771549937" 然后我调用方法getContentType() 我不知道在哪里可以找到带有@符号的代码,我真的不明白你的意思
  • 很高兴你能成功。基本上,以“cid:”开头的图像 url 应该是图像 MIME 部分的 Content-ID 标头,前面带有“cid:”。 “cid:” URL 的基本意思是“图像在此消息中的某处,查找带有此 content-ID 标头的内容”。
猜你喜欢
  • 2017-07-04
  • 2011-10-06
  • 2015-03-14
  • 2016-11-06
  • 2011-10-30
  • 2012-03-30
  • 2022-09-25
相关资源
最近更新 更多