javamail

public void sendMail(Properties properties, SysUser sysUser, String fullPath)
throws Exception {
CommonSendMailService commonSendMailService = (CommonSendMailService) AppUtil
.getBean(CommonSendMailService.class);
Session sendsession = null;
Transport transport = null;
MimeMessage message = null;
try {


String toMail = getCurrUserMaill(sysUser);
if (StringUtil.isEmpty(toMail)) {
throw new Exception("提交该流程员工没有邮箱,请维护邮箱!");
}
Multipart multipart = new MimeMultipart("related");
// 获取邮件连接
sendsession = commonSendMailService.MailConnectSession(properties);

sendsession.setDebug(true);

// 根据Session生成Message对象
message = new MimeMessage(sendsession);
// 设置发信人地址
message.setFrom(new InternetAddress(properties
.getProperty("mail.from")));
// 设置收信人地址
new InternetAddress();
InternetAddress[] iaToList = InternetAddress.parse(toMail);
message.setRecipients(Message.RecipientType.TO, iaToList);


// 设置e-mail标题
message.setSubject("");
// 设置e-mail发送时间
message.setSentDate(new Date());
// 建立第一部分:文本正文
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(getContentText(sysUser),
"text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式
// add content
multipart.addBodyPart(messageBodyPart);
// 创建二维码图片
BodyPart messageBodyPartPic = new MimeBodyPart();


DataSource fds = new FileDataSource(fullPath);
messageBodyPartPic.setDataHandler(new DataHandler(fds));
messageBodyPartPic.setHeader("Content-ID", "<image>");
// add pic
multipart.addBodyPart(messageBodyPartPic);
// 将多部分内容放到e-mail中
message.setContent(multipart);
// 保存对于e-mail的修改
message.saveChanges();
// 根据Session生成Transport对象
transport = sendsession.getTransport("smtp");
// 连接到SMTP服务器
transport.connect(properties.getProperty("mail.host"),
properties.getProperty("mail.username"),
properties.getProperty("mail.password"));
// 发送e-mail
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
throw new Exception("发送邮件失败:"
+ ExceptionUtil.getExceptionMessage(e));
} finally {
// 关闭Transport连接
if (null != transport) {
transport.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值