首页 > 解决方案 > 发送 Multipart MimeMessage 会发送一封空电子邮件

问题描述

我正在从事这个项目,该项目要求我们通过邮件以 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 生成的。

提前致谢!

标签: javajakarta-eejakarta-mailmime-messagetomee-7

解决方案


我没有发现代码有任何问题。

  1. 确保您具有适当版本的 mail.jar activation.jar
  2. 如果可以的话,另一件事是:

    String pdfFileName = "report.pdf"; DataSource source = new FileDataSource(pdfFilename); // DataSource pdfAttachment = new ByteArrayDataSource(pdfBaos.toByteArray(), "application/pdf");


推荐阅读