我是新来的java邮件。我想发送带有图像附件的邮件。我已经尝试了下面的代码来附加邮件的图像。如何用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);
此代码正在工作。邮件与图像附件一起收到。但问题在于,图像以不支持的格式显示或不显示原始图像。
我不知道如何解决这个问题。
请帮助我..提前
谢谢..
你设置MIME类型为'应用程序/八位字节stream' - 不它看起来更好,如果你使用'图像/ PNG'或任何适合的图像格式? –
请分享一些例子。 –
javax.mail库 –