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

发布于 2024-08-08 20:28:06 字数 1003 浏览 5 评论 0原文

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

如果我不调用 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();

I'm using Apache Commons Email 1.1 and 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.

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?

  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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浪漫之都 2024-08-15 20:28:06
email.attach(new ByteArrayDataSource(pdfBytes, "application/pdf"),
      "document.pdf", "Document description",
       EmailAttachment.ATTACHMENT);

这适用于 commons-email 1.1。

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

(上面的是org.apache.commons.mail.ByteArrayDataSource

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

this works with commons-email 1.1.

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

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

美男兮 2024-08-15 20:28:06

请注意,

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

在使用 commons-email 1.1 的 HtmlEmail 上使用:会导致生成的电子邮件将其消息(文本或 html)作为附件包含在内。

切换到 MultiPartEmail 解决了这个问题。

Note that using:

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

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

Switching to a MultiPartEmail fixed this.

征棹 2024-08-15 20:28:06

我建议您尝试当前的候选版本 v1.2 RC2,因为 1.1 (我猜您使用它)有一些 html 布局问题

commons-email 1.2 RC2

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

commons-email 1.2 RC2

梦里寻她 2024-08-15 20:28:06

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

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

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

File pdf = new File(pdfPath);
email.embed(pdf.toURI().toURL(), pdf.getName)
红颜悴 2024-08-15 20:28:06

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

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

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

 email.attach(new FileDataSource(attachmentFileObject), "AttachmentName", "Description");
望笑 2024-08-15 20:28:06

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

  1. HtmlEmail 扩展了 MultiPartEmail,attacch 方法返回
    多部分电子邮件类。
  2. HtmlEmail 将转换为 MultiPartEmail,然后使用
    tempEmail.attach(附件)。
  3. 然后 MultiPartEmail 将被转换回 HtmlEmail 类。

    HtmlEmail 电子邮件;
    多部分电子邮件临时电子邮件;
    
    // HtmlEmail ->;多部分电子邮件
    tempEmail = new HtmlEmail();
    
    // 添加附件
    EmailAttachment 附件 = new EmailAttachment();
    附件.setURL(新URL(文件URL));
    附件.setDisposition(EmailAttachment.ATTACHMENT);
    Attachment.setName(MimeUtility.encodeText(fileName));
    tempEmail = tempEmail.attach(附件);
    
    // 多部分电子邮件 -> HTML电子邮件
    电子邮件 = (HtmlEmail)tempEmail;
    

You can use up-casting and down-casting in 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;
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文