JavaMail base64 编码

发布于 2025-01-01 14:15:21 字数 461 浏览 1 评论 0原文

我有一些 Java 代码,它会发送一封电子邮件,其代码有点类似于以下内容:

MimeBodyPart part = new MimeBodyPart();
part.setContent(htmlString, "text/html; charset=\"UTF-8\"");
part.setHeader("MIME-Version", "1.0");
part.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
part.setHeader("Importance", severityVal);

mimeMultiPart.addBodyPart(htmlPart);
mimeMessage.setContent(mimeMultiPart);

...等等。

如何对这封外发电子邮件的“部分”MimeBodyPart 进行 Base64 编码?

I have some Java code which sends out an email with code somewhat like the following:

MimeBodyPart part = new MimeBodyPart();
part.setContent(htmlString, "text/html; charset=\"UTF-8\"");
part.setHeader("MIME-Version", "1.0");
part.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
part.setHeader("Importance", severityVal);

mimeMultiPart.addBodyPart(htmlPart);
mimeMessage.setContent(mimeMultiPart);

... and so on.

How can I encode the "part" MimeBodyPart in base64 for this outgoing email?

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

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

发布评论

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

评论(2

笨笨の傻瓜 2025-01-08 14:15:21

来自 docs

问:尽管 JavaMail 完成了所有编码和对我来说,解码时,我需要手动控制某些身体部位的编码。
答:在极少数情况下,您需要控制编码,有多种方法可以覆盖 JavaMail 的默认行为。一个简单的方法如下。创建整个消息后,调用 msg.saveChanges() ,然后使用 mbp.setHeader("Content-Transfer-Encoding", "base64") 强制使用 base64给定身体部位的编码。

另一种方法是子类化 MimeBodyPart 并重写 updateHeaders 方法,以便它首先调用 super.updateHeaders(),然后设置 Content -Transfer-Encoding 标头如上。

From the docs:

Q: Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts.
A: In the rare case that you need to control the encoding, there are several ways to override JavaMail's default behavior. A simple approach is as follows. After creating the entire message, call msg.saveChanges() and then use something like mbp.setHeader("Content-Transfer-Encoding", "base64") to force base64 encoding for the given body part.

Another approach is to subclass MimeBodyPart and override the updateHeaders method so that it first calls super.updateHeaders() and then sets the Content-Transfer-Encoding header as above.

柠北森屋 2025-01-08 14:15:21

只需添加以下语句:

part.setHeader("Content-Transfer-Encoding", "base64");

在设置 part.setDataHandler(..)part.setContent(...) 的代码行之后,并且 dataSource/Handler 将进行自我调整以匹配您刚刚为正文部分指定的内容传输编码。

您可能还想考虑通过相同的方式设置标头,例如:

part.setHeader("Content-Type", "application/octet-stream");

simply add this statement:

part.setHeader("Content-Transfer-Encoding", "base64");

after the lines of code that set part.setDataHandler(..) or part.setContent(...), and the dataSource/Handler will adapt itself to match the content transfer encoding you just specified for the body part.

You may also want to consider setting the headers by the same token, e.g.:

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