我是 Java 邮件的新手。我想发送带有图片附件的邮件。我已尝试使用以下代码将图像附加到邮件中。
BodyPart messageBodyPart = new MimeBodyPart();
if (content == null) {
messageBodyPart.setText("");
} else {
messageBodyPart.setText(content);
}
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
// messageBodyPart = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(
attachedFile2.getBytes("UTF-8"),
"application/octet-stream");
//attachedFile2 is the filename of image.
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachedFile2);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
此代码正在运行。收到带有图像附件的邮件。但问题是图像以不支持的格式显示或不显示原始图像。
我不知道如何解决这个问题。
请帮我..
提前致谢..