Java Don';我不想将图像作为附件,但需要将其内联

Java Don';我不想将图像作为附件,但需要将其内联,java,email,Java,Email,我有这段代码使用Java在电子邮件中包含图像 MimeBodyPart messageBodyPart = new MimeBodyPart(); DataSource imageSource = new ByteArrayDataSource(imageToByteArray(image, format), type); messageBodyPart.setDataHandler(new DataHandler(imageSource)); messageBodyPart.setConten

我有这段代码使用Java在电子邮件中包含图像

MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource imageSource = new ByteArrayDataSource(imageToByteArray(image, format), type);
messageBodyPart.setDataHandler(new DataHandler(imageSource));
messageBodyPart.setContentID(imageContentID);
multipart.addBodyPart(messageBodyPart);
但我的图像显示为附件和内联。有什么问题吗?

两件事:

  • 我没有在主体部分设置文件名

    messageBodyPart.setFileName(文件名)

  • 我将格式和类型分别设置为“jpg”、“image/jpg”,而不是“png”、“image/png”

    DataSource imageSource=新的ByteArrayDataSource(imageToByteArray(图像,格式),类型)

  • 可能相关:。