我是新來的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庫 –