发送 HTML 封电子邮件和多个文件附件?

Send HTML email along with multiple file attachments?

我正在尝试在电子邮件正文中发送 HTML 电子邮件以及一些文件附件。我想出了以下代码:

public void sendEmail(final String to, final String from, final String cc, final String subject, final String body,
        final String baseDirectory, final List<String> listOfFileNames) {
    for (int i = 1; i <= 3; i++) { // retrying
        try {
            Session session = Session.getInstance(mailProperties, null);

            Multipart multipart = new MimeMultipart();
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));

            InternetAddress[] toAddress = InternetAddress.parse(to);
            InternetAddress[] ccAddress = InternetAddress.parse(cc);

            message.addRecipients(RecipientType.TO, toAddress);
            message.addRecipients(RecipientType.CC, ccAddress);
            message.setSubject(subject);
            message.setContent(body, "text/html;charset=utf8");

            for (String file : listOfFileNames) {
                String fileLocation = baseDirectory + "/" + file;
                addAttachment(multipart, fileLocation);
            }
            message.setContent(multipart);
            Transport.send(message, toAddress);
            break;
        } catch (Exception ex) {
            // log exception
        }
    }
}

// this is used for attachment
private void addAttachment(final Multipart multipart, final String filename) throws MessagingException {
    DataSource source = new FileDataSource(filename);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
}

我需要将正文字符串作为我的 HTML 电子邮件的一部分,并将所有文件附加到同一封电子邮件中。我在我的主目录中是这样的 运行,我有两个文件:abc.csvtree.txt

EmailTest.getInstance().sendEmail("hello@host.com", "hello@host.com", "hello@host.com",
                "Test Subject (" + dateFormat.format(new Date()) + ")", content, "/export/home/david/",
                Arrays.asList("abc.csv", "tree.txt"));

收到电子邮件后,我在电子邮件正文中根本看不到我的文字?第二件事是文件附件名称即将出现 /export/home/david/abc.csv/export/home/david/tree.txt?

我做错了什么吗?当我用不同的参数两次调用 setContent 方法时,我看到了一件事?

首先您需要将文本添加为​​自己的文本 BodyPart。接下来,您的 MimeMultipart 需要设置为 related 类型,这样您就可以同时拥有 HTML-Text 和一些附件。那么它应该可以同时拥有附件和文本。

您传递给 messageBodyPart.setFileName(filename) 的文件名就是您在附件名称中看到的文件名。所以只要省略路径,你应该只看到 abc.csvtree.txt

public void sendEmail(final String to, final String from, final String cc, final String subject, final String body,
        final String baseDirectory, final List<String> listOfFileNames) {
    for (int i = 1; i <= 3; i++) { // retrying
        try {
            Session session = Session.getInstance(mailProperties, null);

            Multipart multipart = new MimeMultipart("related");
            MimeBodyPart bodyPart= new MimeBodyPart();

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));

            InternetAddress[] toAddress = InternetAddress.parse(to);
            InternetAddress[] ccAddress = InternetAddress.parse(cc);

            message.addRecipients(RecipientType.TO, toAddress);
            message.addRecipients(RecipientType.CC, ccAddress);
            message.setSubject(subject);

            bodyPart.setText(body, "UTF-8", "html");
            multipart.addBodyPart(bodyPart);

            for (String file : listOfFileNames) {
                String fileLocation = baseDirectory + "/" + file;
                addAttachment(multipart, fileLocation, file);
            }
            message.setContent(multipart);
            Transport.send(message, toAddress);
            break;
        } catch (Exception ex) {
            // log exception
        }
    }
}

// this is used for attachment
private void addAttachment(final Multipart multipart, final String filepath, final String filename) throws MessagingException {
    DataSource source = new FileDataSource(filepath);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
}