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();
}
}
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();
}
}