try {
msg.setSubject(MimeUtility.encodeText(this.title));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new FixFlowException("邮件标题转换失败!", e);
}
if (!hasAttachment()) {
// 如果没有带附件
if (this.isHtmlModeMail()) {
// 是HTML格式的邮件
if (!this.hasHtmlDesc()) {
msg.setContent(this.content, this.content_type);
} else {
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = null;
mbp = new MimeBodyPart();
mbp.setContent(this.content, this.content_type);
mp.addBodyPart(mbp);
mbp = new MimeBodyPart();
mbp.setContent(this.htmlMailDesc, this.MODE_TEXT);
mp.addBodyPart(mbp);
msg.setContent(mp);
}
} else {
// 是文本格式的邮件
msg.setText(this.content);
}
} else {
// 有附件
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = null;
// 邮件正文
for (int i = 0; i < vFiles.size(); i++) {
mbp = new MimeBodyPart();
File file = (File) vFiles.get(i);
FileDataSource fds = new FileDataSource(file);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(file.getName());// //////////////////
mp.addBodyPart(mbp);
}
for (int i = 0; i < vURLs.size(); i++) {
mbp = new MimeBodyPart();
URL url = (URL) vURLs.get(i);
// URLDataSource uds=new URLDataSource(url);
mbp.setDataHandler(new DataHandler(url));
mbp.setFileName(url.getFile());// //////////////////
mp.addBodyPart(mbp);
}
mbp = new MimeBodyPart();
mbp.setContent(this.content, this.content_type);
mp.addBodyPart(mbp);
if (this.isHtmlModeMail() && this.hasHtmlDesc()) {
mbp = new MimeBodyPart();
mbp.setContent(this.htmlMailDesc, this.MODE_TEXT);
mp.addBodyPart(mbp);
}
msg.setContent(mp);
}
msg.saveChanges();
if (this.isAuthenticationSMTP) {
Transport transport = conn.getTransport("smtp");
transport.connect(this.smtpHost, this.smtpUser, this.smtpPassword);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} else {
Transport.send(msg, msg.getAllRecipients());
}
} catch (javax.mail.internet.AddressException e) {
e.printStackTrace();
throw new FixFlowException(e.getMessage(), e);
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
throw new FixFlowException(e.getMessage(), e);
}
}