java 如何使用 Apache Commons Email 将文件附加到 HTML 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1610301/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:16:50  来源:igfitidea点击:

How to attach a file to an HTML email using Apache Commons Email

javaemailapache-commons-email

提问by delux247

I'm using Apache Commons Email 1.1and I can't figure out how to attach a file to an HtmlEmail. If I run the code below, I get an email with an attachment, but the HTML message comes across as an attachment also.

我正在使用Apache Commons Email 1.1,但不知道如何将文件附加到 HtmlEmail。如果我运行下面的代码,我会收到一封带有附件的电子邮件,但 HTML 消息也作为附件出现。

If I don't call email.attach() the HTML message come through as you would expect, but I need both the HTML message to come through and the attachment. What am I missing?

如果我不调用 email.attach(),HTML 消息会按您的预期通过,但我需要 HTML 消息和附件。我错过了什么?

  HtmlEmail email = new HtmlEmail();
  email.setHostName("localhost");
  email.addTo("[email protected]", "Test");
  email.setFrom("[email protected]", "Test App");
  email.setSubject("Test message");
  email.setHtmlMsg("<div style='font-size: 20px; color: green;'>This is html email</div>");

  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath(pdfPath);
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  email.attach(attachment);

  email.send();

回答by Bozho

email.attach(new ByteArrayDataSource(pdfBytes, "application/pdf"),
      "document.pdf", "Document description",
       EmailAttachment.ATTACHMENT);

this works with commons-email 1.1.

这适用于 commons-email 1.1。

pdfBytesshould be a byte[]containing the bytes of the pdf document. If that doesn't suit you, you can try other DataSourceimplementations, but I can't guarantee they'd work (although they should).

pdfBytes应该是一个byte[]包含 pdf 文档的字节。如果这不适合您,您可以尝试其他DataSource实现,但我不能保证它们会起作用(尽管它们应该)。

(The one above is org.apache.commons.mail.ByteArrayDataSource)

(上面那个是org.apache.commons.mail.ByteArrayDataSource

回答by Thomas N

Note that using:

请注意,使用:

email.attach(new ByteArrayDataSource(pdfBytes, "application/pdf"),
      "document.pdf", "Document description",
       EmailAttachment.ATTACHMENT);

on a HtmlEmailusing commons-email 1.1 causes the resulting email to have its message (text or html) enclosed as an attachment.

HtmlEmail使用公共资源的电子邮件1.1导致所得的电子邮件具有其封闭的作为附件的消息(文本或HTML)。

Switching to a MultiPartEmailfixed this.

切换到一个MultiPartEmail固定的。

回答by jitter

I suggest you try the current release candidate v1.2 RC2 as 1.1 (I guess you use that) has some html layout problems

我建议你试试当前的候选版本 v1.2 RC2 作为 1.1(我猜你用的)有一些 html 布局问题

commons-email 1.2 RC2

公共电子邮件 1.2 RC2

回答by Ji ZHANG

I use the HtmlEmail#embed(URL, String) method:

我使用 HtmlEmail#embed(URL, String) 方法:

File pdf = new File(pdfPath);
email.embed(pdf.toURI().toURL(), pdf.getName)

回答by Cybermonk

On latest release (1.5) the following code worked for me

在最新版本 (1.5) 上,以下代码对我有用

 email.attach(new FileDataSource(attachmentFileObject), "AttachmentName", "Description");

回答by JamesR

You can use up-casting and down-casting in Java.

您可以在 Java 中使用向上转换和向下转换。

  1. HtmlEmail extends MultiPartEmail, and attacch method returns the MultiPartEmail class.
  2. HtmlEmail will be converted to MultiPartEmail, and then use tempEmail.attach (attachment).
  3. Then MultiPartEmail will be converted back to HtmlEmail class.

    HtmlEmail email;
    MultiPartEmail tempEmail;
    
    // HtmlEmail -> MultiPartEmail
    tempEmail = new HtmlEmail();
    
    // Add a attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(new URL(fileURL));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(MimeUtility.encodeText(fileName));
    tempEmail = tempEmail.attach(attachment);
    
    // MultiPartEmail -> HtmlEmail
    email = (HtmlEmail)tempEmail;
    
  1. HtmlEmail 扩展了 MultiPartEmail,attacch 方法返回 MultiPartEmail 类。
  2. 将HtmlEmail 转换为MultiPartEmail,然后使用tempEmail.attach(附件)。
  3. 然后 MultiPartEmail 将被转换回 HtmlEmail 类。

    HtmlEmail email;
    MultiPartEmail tempEmail;
    
    // HtmlEmail -> MultiPartEmail
    tempEmail = new HtmlEmail();
    
    // Add a attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(new URL(fileURL));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(MimeUtility.encodeText(fileName));
    tempEmail = tempEmail.attach(attachment);
    
    // MultiPartEmail -> HtmlEmail
    email = (HtmlEmail)tempEmail;