我正在使用javamail api,我必须通过Exchange服务器发送电子邮件,然后将图像嵌入到电子邮件正文中。如何使用Exchange将MimeMultipart中的嵌入图像发送到EmailMessage中?我已经使用SMTP成功发送了同一封电子邮件。
EmailMessage msg = new EmailMessage(service);
msg.setSubject(emailSubject);
msg.getReplyTo().add(emailAdmin);
msg.getToRecipients().add(emailAddress);
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");
//1st part (the message)
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message,"text/html; charset=UTF-8");
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataHandler h = null;
String base64 = configuration.getImageBase64();
//Base64.decodeBase64(string | bytes)
byte[] decode = Base64.decodeBase64(base64.getBytes());
InputStream stream = new ByteArrayInputStream(decode);
try {
h=new DataHandler(new ByteArrayDataSource(stream,"application/octet-stream"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
messageBodyPart.setDataHandler(h);
messageBodyPart.setHeader("Content-ID", "image");
messageBodyPart.setHeader("Content-Type", "image/jpeg; name=image.jpg");
messageBodyPart.setHeader("Content-Disposition", "inline");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
msg.setContent(multipart);////how to set multipart into msg?
synchronized (msg) {
msg.send();
}