java发送带图片的邮件代码
图片目录:
实现代码:
/**
* 发送带附件的mail
*
* @param subject 邮件标题
* @param to 收件人列表
* @param cc 抄送人列表
* @param bodyText 邮件内容
* @param attachmentPaths 附件地址列表
* @throws Exception
*/
public void send(String subject, List<String> to, List<String> cc, String bodyText, String[] attachmentPaths)
throws Exception {
ExchangeService service = getExchangeService();
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
body.setBodyType(BodyType.HTML);
msg.setBody(body);
InputStream image001 = this.getClass().getResourceAsStream("/mail/image001(12-10-14-54-57).png");
msg.getAttachments().addFileAttachment("image001(12-10-14-54-57).png", image001);
//msg.getAttachments().getItems().get(0).setIsInline(true);
msg.getAttachments().getItems().get(0).setContentId("image001(12-10-14-54-57).png");
InputStream image005 = this.getClass().getResourceAsStream("/mail/image005(12-10-14-54-57).jpg");
msg.getAttachments().addFileAttachment("image005(12-10-14-54-57).jpg", image005);
//msg.getAttachments().getItems().get(1).setIsInline(true);
msg.getAttachments().getItems().get(1).setContentId("image005(12-10-14-54-57).jpg");
for (String toPerson : to) {
msg.getToRecipients().add(toPerson);
}
if (cc != null) {
for (String ccPerson : cc) {
msg.getCcRecipients().add(ccPerson);
}
}
if (attachmentPaths != null) {
for (String attachmentPath : attachmentPaths) {
msg.getAttachments().addFileAttachment(attachmentPath);
}
}
msg.send();
}