如何使用 Apache Commons Email 将文件附加到 HTML 电子邮件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这适用于 commons-email 1.1。
pdfBytes
应该是包含 pdf 文档字节的byte[]
。如果这不适合您,您可以尝试其他DataSource
实现,但我不能保证它们能够工作(尽管它们应该)。(上面的是
org.apache.commons.mail.ByteArrayDataSource
)this works with commons-email 1.1.
pdfBytes
should be abyte[]
containing the bytes of the pdf document. If that doesn't suit you, you can try otherDataSource
implementations, but I can't guarantee they'd work (although they should).(The one above is
org.apache.commons.mail.ByteArrayDataSource
)请注意,
在使用 commons-email 1.1 的
HtmlEmail
上使用:会导致生成的电子邮件将其消息(文本或 html)作为附件包含在内。切换到 MultiPartEmail 解决了这个问题。
Note that using:
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.我建议您尝试当前的候选版本 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
我使用 HtmlEmail#embed(URL, String) 方法:
I use the HtmlEmail#embed(URL, String) method:
在最新版本(1.5)上,以下代码对我有用
On latest release (1.5) the following code worked for me
您可以在 Java 中使用向上转换和向下转换。
多部分电子邮件类。
tempEmail.attach(附件)。
然后 MultiPartEmail 将被转换回 HtmlEmail 类。
You can use up-casting and down-casting in Java.
MultiPartEmail class.
tempEmail.attach (attachment).
Then MultiPartEmail will be converted back to HtmlEmail class.