Java 内联附件问题

Java 内联附件问题,java,outlook,jakarta-mail,Java,Outlook,Jakarta Mail,我正在尝试发送一封带有内联图像的java电子邮件。 问题是,从电子邮件预览中,Outlook向我显示邮件有附件,如图所示 我将附件添加到电子邮件中,如下所示: private void addFilesToEmailSMTP2(MimeMessage msg) throws MessagingException { MimeMultipart multipart = new MimeMultipart("related"); MimeBodyPart mess

我正在尝试发送一封带有内联图像的java电子邮件。 问题是,从电子邮件预览中,Outlook向我显示邮件有附件,如图所示

我将附件添加到电子邮件中,如下所示:

private void addFilesToEmailSMTP2(MimeMessage msg) throws MessagingException {

        MimeMultipart multipart = new MimeMultipart("related");

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(testo, "text/html");

        multipart.addBodyPart(messageBodyPart);

        for (Entry<String, String> a : attach.entrySet()) {//attach is pre populated with key: content-id, value: base64 encoded image

            messageBodyPart = new MimeBodyPart();

            try {
                byte[] byteArray = Base64.getDecoder().decode(a.getValue());

                File tempFile = File.createTempFile("tmpFile", null);

                try (FileOutputStream fos = new FileOutputStream(tempFile);) {
                    fos.write(byteArray);
                }

                DataSource fds = new FileDataSource(tempFile);

                messageBodyPart.setDataHandler(new DataHandler(fds));
                messageBodyPart.setContentID(a.getKey());
                messageBodyPart.setDisposition(MimeBodyPart.INLINE);
                log.info("Adding attach {}", a.getKey());

                multipart.addBodyPart(messageBodyPart);

            } catch (MessagingException | IOException e) {
                log.error(threadName + " Attachment " + a.getKey() + " threw error: " + e.getMessage(), e);
            }
        }


        msg.setContent(multipart);

    }
private void addFilesToEmailSMTP2(MimeMessage msg)抛出消息异常{
MimMultipart multipart=新的MimMultipart(“相关”);
MimeBodyPart messageBodyPart=新的MimeBodyPart();
setContent(testo,“text/html”);
multipart.addBodyPart(messageBodyPart);
对于(条目a:attach.entrySet()){//attach预先填充了key:content-id,value:base64编码图像
messageBodyPart=新的MimeBodyPart();
试一试{
字节[]byteArray=Base64.getDecoder().decode(a.getValue());
File tempFile=File.createTempFile(“tmpFile”,null);
try(FileOutputStream fos=newfileoutputstream(tempFile);){
fos.write(byteArray);
}
DataSource fds=新文件DataSource(tempFile);
setDataHandler(新的DataHandler(fds));
messageBodyPart.setContentID(a.getKey());
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
info(“添加attach{}”,a.getKey());
multipart.addBodyPart(messageBodyPart);
}捕获(消息异常| IOE异常){
log.error(threadName+“附件”+a.getKey()+“抛出错误:”+e.getMessage(),e);
}
}
msg.setContent(多部分);
}

电子邮件结果正常,附件隐藏且图像在正文中,但outlook不断向我显示带有

outlook的电子邮件可能假定任何多部分邮件都有附件。您可以尝试在html中以内联数据的形式发送图像,如中所述。谢谢您的回答,我会尝试的。最奇怪的是,我使用的是Magnolia cms电子邮件库,但这不会发生。。Magnolia库所基于的Idk,但我认为是在Java Mail上。比较他们生成的邮件的MIME内容,看看有什么不同。Outlook可能假设任何多部分邮件都有附件。您可以尝试在html中以内联数据的形式发送图像,如中所述。谢谢您的回答,我会尝试的。最奇怪的是,我使用的是Magnolia cms电子邮件库,但这不会发生。。Magnolia基于哪个Idk库,但我认为是在Java Mail上。比较他们生成的消息的MIME内容,看看有什么不同。