java - 使用java邮件将图像嵌入到html电子邮件中

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

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

我还想提一下 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\"/>

最佳答案

错误是因为<img src=\"cid:logoimg\"/>

应该有:imgPart.setHeader("Content-ID", "<logoimg>");

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

即:您需要“<”和“>”

关于java - 使用java邮件将图像嵌入到html电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5260654/

相关文章:

java - 在多模块项目中使用 Liquibase

javascript - 切换 div 以放大并向左滑动

java - JSF a4j :ajax and f:ajax are failing when rendering

java - 如何在 Pushwoosh 中处理已删除的应用程序设备 token ?

html - 如何防止按钮溢出?

javascript - 使用 JavaScript 根据 JSON 文件开头的内容读取该行

hibernate - org.hibernate.exception.GenericJDBCException : could not get next sequence value

Apache shiro 属性 'sessionManager.globalSessionTimeout' 不存在

java - 从方法 a() 调用方法 b() 时是否开始新事务?

java - 如何在状态为 Activity 时回滚表并在状态为挂起时删除表?