如何将图片放在邮件标题中(春季启动)?

时间:2020-07-19 19:59:23

标签: java spring-boot jakarta-mail html-email

我正在开发一个应用程序,需要在邮件的页眉和页脚中设置图像。我看到了方法setHeader(String header-name, String header_value),但是当我将其放入图像的路径时,却什么也没得到。 这是我的代码:

public static void send(String host, String port,
            final String userName, final String password, String toAddress,
            String subject, String htmlBody,
            Map<String, String> mapInlineImages)
                throws AddressException, MessagingException {
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.user", userName);
        properties.put("mail.password", password);
 
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        Session session = Session.getInstance(properties, auth);
 
        // creates a new e-mail message
        Message msg = new MimeMessage(session);
 
        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setHeader("image1", "D:\\broki\\src\\main\\resources\\imageHeader.jpg");
 
        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(htmlBody, "text/html");
 
        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
 
        // adds inline image attachments
        if (mapInlineImages != null && mapInlineImages.size() > 0) {
            Set<String> setImageID = mapInlineImages.keySet();
             
            for (String contentId : setImageID) {
                MimeBodyPart imagePart = new MimeBodyPart();
                imagePart.setHeader("Content-ID", "<" + contentId + ">");
                imagePart.setDisposition(MimeBodyPart.INLINE);
                 
                String imageFilePath = mapInlineImages.get(contentId);
                try {
                    imagePart.attachFile(imageFilePath);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
 
                multipart.addBodyPart(imagePart);
            }
        }
 
        msg.setContent(multipart);
 
        Transport.send(msg);
    }
}

您可以部分看到

msg.setHeader("image1", "D:\\broki\\src\\main\\resources\\imageHeader.jpg")

我将一些键和值(它们是我要设置的图像的路径)放入标题中。但是什么也没发生。有人可以帮我吗?

1 个答案:

答案 0 :(得分:3)

"header" in a MIME message一词指的是消息中具有“键:值”字段的部分。此部分称为“标头”,因为它是在消息的“正文”内容之前发送的。标题用于“日期”,“主题”,“内容类型”之类的内容。

“页眉”一词不表示在屏幕上或打印时可见的graphical header。要添加这种类型的标题,您必须修改消息的内容。在您的程序中,该变量存储在htmlBody变量中。

我认为您不知道其结构如何就可以简单地修改htmlBody来添加标题。在一般情况下,必须从头开始将页面标题并入HTML消息设计中。