使用 Java 发送带有 pdf 附件的 HTML 电子邮件
我正在尝试使用 Java 发送电子邮件,我正在使用 apache commons 电子邮件库。
我无法发送正文为 HTML 且附件为 PDF(或任何文件类型)的电子邮件
如果我使用 EmailAttachment() 并将其添加到 HtmlEmail 对象,我的邮件看起来像有两个附件。第一个用于 HTML,第二个用于 PDF。
有什么办法可以做到这一点吗?
非常感谢!
I am trying to send an email with Java, I am using apache commons email library.
I cannot achieve to send an email with a body as HTML and an attachment as PDF (or any file type)
If I use EmailAttachment() and add it to an HtmlEmail object, my mail looks like with two attachment. First one is for HTML, second is for PDF.
Is there any way to do that?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于以文本和 HTML 形式发送的消息和/或更喜欢文本电子邮件的邮件客户端(在接收端)来说,这听起来是相对正常的行为。我怀疑这是由于客户端的行为造成的,您无法更改该行为(但从好的方面来说,所有 HTML 电子邮件都会像这样显示)。
事实是,HTML 电子邮件(带有文本组件)实际上是多部分消息,其中 HTML 内容是“额外”部分之一。您实际上从服务器端发送的电子邮件只是一堆文本,由接收邮件客户端决定如何显示它。在这方面,客户端将 HTML 显示为附件并没有错 - 就像智能客户端推断 HTML 不是“真正的”附件并激活文本之间的某种切换并没有错一样和 HTML(而不是将其显示为附件)。
如果您确信客户端通常会以这种智能方式处理 HTML,那么:
It sounds like relatively normal behaviour for a message that's being sent as both text and HTML, and/or a mail client (at the receiving end) that prefers text emails. I suspect that this is due to the behaviour of the client, which you won't be able to change (but on the plus side all HTML emails would appear like this).
The thing is, an HTML email (with a textual component) really is a multipart message, with the HTML content as one of the "extra" parts. All you're actually sending in the email from the server side is a bunch of text, and it's up to the receiving mail client to decide how to display it. In that respect, it is not wrong for the client to display your HTML as an attachment - just like it is not wrong for a smart client to infer that the HTML isn't a "real" attachment and activate some kind of toggle between text and HTML (rather than displaying it as an attachment).
If you're convinced that the client would normally treat HTML in this smart way, then:
我尝试了 apache commons mail v1.2 而不是 1.1。
有用!?
Andrej,顺便说一句,非常感谢您的善意帮助。
I tried apache commons mail v1.2 instead of 1.1.
It works!?
Andrej, by the way many thanks for your kindly help.