发送HTML电子邮件以及多个文件附件?

时间:2015-09-12 16:52:37

标签: java email javamail email-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);
}

我需要将body String作为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方法两次?

1 个答案:

答案 0 :(得分:1)

首先,您需要将文本添加为​​自己的BodyPart。接下来,您的MimeMultipart需要设置为r elated类型,这样您就可以同时拥有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);
}