我正在从事这个项目,要求我们通过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生成的。
谢谢!
答案 0 :(得分:0)
我发现代码没有任何问题。
您可以尝试的另一件事是:
String pdfFileName = "report.pdf";
DataSource source = new FileDataSource(pdfFilename);
// DataSource pdfAttachment = new ByteArrayDataSource(pdfBaos.toByteArray(), "application/pdf");