【发布时间】:2018-12-08 08:19:25
【问题描述】:
我正在从事这个项目,该项目要求我们通过邮件发送 pdf 和 xlsx 格式的每日报告。我读过关于使用 Multipart 消息来完成这样的任务,所以我在下面编写了一个代码来完成它:
public void sendEmail(String toEmail, String subject, String body, ByteArrayOutputStream pdfBaos, ByteArrayOutputStream xlsBaos) {
System.out.println("TLSEmail Start");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); // SMTP Host
props.put("mail.smtp.port", "587"); // TLS Port
props.put("mail.smtp.auth", "true"); // enable authentication
props.put("mail.smtp.starttls.enable", "true"); // enable STARTTLS
Authenticator auth = new Authenticator() {
// override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
try {
MimeMessage msg = new MimeMessage(session);
// set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(Config
.getProperty("reporter.mail.username"), "DailyReport"));
msg.setReplyTo(InternetAddress.parse(
Config.getProperty("reporter.mail.username"), false));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toEmail, false));
msg.setSubject(subject, "UTF-8");
msg.setSentDate(new Date());
// Create a multipart message for attachment
Multipart multipart = new MimeMultipart();
// Create the message body part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(body);
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Second part is attachment
MimeBodyPart pdfAttachmentBodyPart = new MimeBodyPart();
DataSource pdfAttachment = new ByteArrayDataSource(pdfBaos.toByteArray(), "application/pdf");
String pdfFileName = "report.pdf";
pdfAttachmentBodyPart.setDataHandler(new DataHandler(pdfAttachment));
pdfAttachmentBodyPart.setFileName(pdfFileName);
MimeBodyPart xlsAttachmentBodyPart = new MimeBodyPart();
DataSource xlsAttachment = new ByteArrayDataSource(xlsBaos.toByteArray(), "application/vnd.ms-excel");
String xlsFileName = "report.xls";
xlsAttachmentBodyPart.setDataHandler(new DataHandler(xlsAttachment));
xlsAttachmentBodyPart.setFileName(xlsFileName);
multipart.addBodyPart(pdfAttachmentBodyPart);
multipart.addBodyPart(xlsAttachmentBodyPart);
// Send the complete message parts
msg.setContent(multipart);
// msg.setText(body, "UTF-8");
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
} catch (Exception e) {
e.printStackTrace();
}
}
但这只会向所需地址发送一封空电子邮件。现在我怀疑我发送文件的方式有问题,所以我尝试只发送文本 bodyPart,但发送的邮件也是空的。我还尝试使用“mixed”参数创建 MimeMultipart,但这并没有解决任何问题。
通过 setText 方法将文本设置为 MimeMessage 效果很好,但这给我留下了如何发送 .pdf 和 .xlsx 附件的问题。
我正在使用 javax.mail 1.5.0-b01 和 apache TomEE 7.0。报告是通过 jasperreports 生成的。
提前致谢!
【问题讨论】:
-
您的代码大部分看起来都不错,但您可能希望通过修复其中任何common mistakes 来使其变得更好。如果可能的话,你可能想upgrade to the latest version of JavaMail, currently 1.6.1。然后,发布JavaMail debug output,以便我们了解发生了什么。
标签: java jakarta-ee jakarta-mail mime-message tomee-7