如何使用JavaMail发送带有附件的html电子邮件

时间:2015-08-05 07:23:35

标签: java html5 javamail

以下Java代码用于将文件附加到 html 电子邮件并发送。我想通过此 html 电子邮件发送附件。任何建议将不胜感激。

axis=1

这只给我带来了附件。但我想发送带有此附件的HTML电子邮件。

2 个答案:

答案 0 :(得分:19)

使用HTML正文和附件创建邮件实际上意味着创建一个内容为“多部分实体”的邮件,其中包含两部分,其中一部分是HTML内容,第二部分是附加文件。 / p>

这与您当前的代码不对应:

Multipart multipart = new MimeMultipart(); // creating a multipart is OK

// Creating the first body part of the multipart, it's OK
messageBodyPart = new MimeBodyPart();
// ... bla bla
// ok, so this body part is the "attachment file"
messageBodyPart.setDataHandler(new DataHandler(source));
// ... bla bla
multipart.addBodyPart(messageBodyPart); // at this point, the multipart contains your file attachment, but only that!

// at this point, you set your mail's body to be the HTML message    
message.setContent(html, "text/html; charset=utf-8");
// and then right after that, you **reset** your mail's content to be your multipart, which does not contain the HTML
message.setContent(multipart);

此时,您的电子邮件内容是一个只有一部分的多部分,这是您的附件。

因此,要达到预期的结果,您应该采用不同的方式:

  1. 创建一个多部分(就像你一样)
  2. 创建一个部分,将您的文件附件作为内容(与您一样)
  3. 将第一部分添加到多部分(正如您所做)
  4. 创建第二个MimeBodyPart
  5. 将您的html内容添加到第二部分
  6. 将此第二部分添加到您的多部分
  7. 将您的电子邮件内容设置为多部分(就像您所做的那样)
  8. 大致翻译为:

    Multipart multipart = new MimeMultipart(); //1
    // Create the attachment part
    BodyPart attachmentBodyPart = new MimeBodyPart(); //2
    attachmentBodyPart.setDataHandler(new DataHandler(fileDataSource)); //2
    attachmentBodyPart.setFileName(file.getName()); // 2
    multipart.addBodyPart(attachmentBodyPart); //3
    // Create the HTML Part
    BodyPart htmlBodyPart = new MimeBodyPart(); //4
    htmlBodyPart.setContent(htmlMessageAsString , "text/html"); //5
    multipart.addBodyPart(htmlBodyPart); // 6
    // Set the Multipart's to be the email's content
    message.setContent(multipart); //7
    

答案 1 :(得分:0)

**Send Email using html body and file attachement**

          > try {
            host = localhost;//use your host or pwd if any
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.setProperty("mail.smtp.auth", "false");

            Session session = Session.getInstance(props, null);
            MimeMessage mailer = new MimeMessage(session);

            // recipient
            mailer.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toEmail));
            // sender
            mailer.setFrom(new InternetAddress(fromEmail));
            // cc
            if (ccEmail != null) {
                for (int i = 0; i < ccEmail.size(); i++) {
                    String cc = ccEmail.get(i);
                    mailer.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(cc));
                }
            }
            Transport t = session.getTransport("smtp");
            t.connect();

            mailer.setSubject(subject);
            mailer.setFrom(new InternetAddress(fromEmail));

            // attachement
            Multipart multipart = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();
            BodyPart attachmentBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(htmlbody, "text/html"); // 5
            multipart.addBodyPart(messageBodyPart);

            // file path
            File filename = new File(xmlurl);
            DataSource source = new FileDataSource(filename);

            attachmentBodyPart.setDataHandler(new DataHandler(source));

            attachmentBodyPart.setFileName(filename.getName());

            multipart.addBodyPart(attachmentBodyPart);

            mailer.setContent(multipart);

            Transport.send(mailer);
            System.out.println("Mail completed");
         } catch (SendFailedException e) {//errr
            }
        }