使用 Java 发送带有 pdf 附件的 HTML 电子邮件

发布于 2024-10-15 18:12:38 字数 224 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

无妨# 2024-10-22 18:12:39

对于以文本和 HTML 形式发送的消息和/或更喜欢文本电子邮件的邮件客户端(在接收端)来说,这听起来是相对正常的行为。我怀疑这是由于客户端的行为造成的,您无法更改该行为(但从好的方面来说,所有 HTML 电子邮件都会像这样显示)。

事实是,HTML 电子邮件(带有文本组件)实际上是多部分消息,其中 HTML 内容是“额外”部分之一。您实际上从服务器端发送的电子邮件只是一堆文本,由接收邮件客户端决定如何显示它。在这方面,客户端将 HTML 显示为附件并没有错 - 就像智能客户端推断 HTML 不是“真正的”附件并激活文本之间的某种切换并没有错一样和 HTML(而不是将其显示为附件)。

如果您确信客户端通常会以这种智能方式处理 HTML,那么:

  1. 您必须提及您正在使用哪个客户端来检查,因为这实际上并不是发送本身的问题;您
  2. 可能需要查看“有效”的电子邮件的原始来源和无效的电子邮件的原始来源,以确定触发不同呈现模式的关键差异是什么。根据客户端软件的不同,这可能是任何东西 - 但我会特别注意部分 MIME 类型和字符集。

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:

  1. You'll have to mention which client you're using to check, because this isn't really an issue with the sending per se; and
  2. You might want to take a look at the raw source of email that "works", and your email that doesn't, in order to determine what the critical differences are that trigger the different rendering modes. Depending on the client software, this could be just about anything - but I'd pay particular attention to part MIME types and charsets.
找个人就嫁了吧 2024-10-22 18:12:39

我尝试了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文