使用Java Mail将图像嵌入到HTML电子邮件中

sfrj:

我正在使用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 | [email protected] | +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 = "[email protected]";

    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\"/>
Ben :

您是否检查过内容类型正确且图像内容处置设置为内联?

另外,Content-ID必须是全局唯一的,您不能只说“ logoimg”。尝试[email protected]但这可能不是您的问题。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用VBA将图像嵌入到Outlook电子邮件中

使用PHP将图像嵌入电子邮件中

无法使用MailKit将图像嵌入电子邮件中

如何使用Java邮件发送html电子邮件时嵌入多个图像

Java Mail:内嵌图像未显示在电子邮件中

如何使用 PHP 在电子邮件中嵌入 HTML 代码

使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?

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

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

将图像附加到React Native中的电子邮件中(使用rn-image-picker和rn-mail)

如何使用javax.mail.jar将文件名添加到电子邮件附件中的图像

如何使用Exchangelib库python将图像嵌入电子邮件正文中

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

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

如何使用python在电子邮件中添加多个嵌入图像

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

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

使用Appscript将3D图表从Google表格插入到电子邮件中作为图像

使用Symfony Mailer将Webpack资产嵌入Twig的模板化电子邮件中

使用python 3在电子邮件中嵌入Pandas df html表

将图像嵌入html电子邮件中

如何在Laravel 4.2中使用Gmail将存储在数组中的电子邮件信息传递到Mail :: send

使用 Access VBA 将 OLE 对象图像插入 html 电子邮件

如何使用HTML文本将图像作为绝对位置发送到电子邮件

无法在ExtentReport中查看使用JavaMail API发送到其他电子邮件ID的嵌入式图像?

删除Java Mail中已删除的电子邮件

我无法使用Java Mail API发送电子邮件,但电子邮件已发送但未收到或未显示在已发送的电子邮件中

使用Outlook 2010将Excel文件作为图像插入电子邮件中

PHP同时使用PHP Mailer将内容循环到电子邮件中