java 如何使用 JavaMail 发送带有附件的 html 电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31825939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send a html email with attached file using JavaMail
提问by Terance
The following Java code is used to attach a file to a htmlemail and send it. I want to send attachment with this htmlemail. Any suggestions would be appreciated.
以下 Java 代码用于将文件附加到html电子邮件并发送。我想用这个html电子邮件发送附件。任何建议,将不胜感激。
public void sendEmail(final String userName, final String password, final String host, final String html, final List<String> emails, String subject, String file) throws MessagingException
{
System.out.println("User Name: " + userName);
System.out.println("Password: " + password);
System.out.println("Host: " + host);
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
});
if (!emails.isEmpty())
{
//Compose the message
InternetAddress[] address = new InternetAddress[emails.size()];
for (int i = 0; i < emails.size(); i++)
{
address[i] = new InternetAddress(emails.get(i));
}
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(userName));
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String fileName = "attachmentName";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(html, "text/html; charset=utf-8");
message.setContent(multipart);
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} else
{
System.out.println("No Recieptions");
}
}
This brings me just only the attachment . But i want to send html email with this attachment .
这给我带来的只是附件。但我想用这个附件发送 html 电子邮件。
回答by GPI
Creating a mail with an HTML body and an attachment, actually means creating a mail whose content is a "multipart entity", that contains two parts, one of them being the HTML content, et the second being the attached file.
创建带有 HTML 正文和附件的邮件,实际上意味着创建内容为“多部分实体”的邮件,该邮件包含两部分,其中一部分是 HTML 内容,第二部分是附件。
This does not correspond to your current code:
这与您当前的代码不符:
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);
At this point, your email's content is a multipart that has only 1 part, which is your attachment.
此时,您的电子邮件内容是一个多部分,只有 1 个部分,即您的附件。
So, to reach your expected result, you should proceed differently :
因此,要达到您的预期结果,您应该采取不同的方式:
- Create a multipart (as you did)
- Create a part, that has your file attachment as a content (as you did)
- Add this first part to the multipart (as you did)
- Create a second
MimeBodyPart
- Add your html content to that second part
- Add this second part to your multipart
- Set your email's content to be the multipart (as you did)
- 创建一个多部分(就像你所做的那样)
- 创建一个零件,将您的文件附件作为内容(如您所做的那样)
- 将第一部分添加到多部分(如您所做的那样)
- 创建第二个
MimeBodyPart
- 将您的 html 内容添加到第二部分
- 将此第二部分添加到您的多部分
- 将您的电子邮件的内容设置为多部分(就像您所做的那样)
Which roughly translates to :
这大致翻译为:
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