如何包含 £ java邮件中电子邮件主题行中的井号符号

发布于 2024-12-01 16:11:11 字数 58 浏览 1 评论 0原文

如何在通过 java 邮件发送的电子邮件的主题行中包含井号?

我发送的时候显示不正确。

How can I include a pound symbol in the subject line of an email sent via java mail?

It shows incorrectly when I sent it.

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

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

发布评论

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

评论(2

一身仙ぐ女味 2024-12-08 16:11:11

主题是标题。标头仅使用 ascii-7,因此要正确编码非 ascii-7 字符,您应该使用正确的编码。

如果您使用的类允许您指定某种编码,请尝试使用 UTF-8。

mimeMessage.setSubject(yourSubject, "UTF-8");

如果您手动编写标题,请使用以下任何一个:

MimeUtility.encodeWord(yourSubject, "UTF-8", "B"); // base-64
MimeUtility.encodeWord(yourSubject, "UTF-8", "Q"); // quoted-printable

这或多或少是 MimeMessage 在 setSubject(str,encoding) 中所做的事情:

setHeader("Subject", MimeUtility.fold(9, MimeUtility.encodeText(subject, charset, null)));
// fold splits the value in several lines with no more than 72 chars

Sample

我已经尝试过这个:

public static void main(String[] args) throws Exception {
            // manual encoding
        System.out.println(MimeUtility.encodeText("How to include £ pound symbol", "UTF-8", "Q"));
        System.out.println(MimeUtility.encodeText("How to include £ pound symbol", "UTF-8", "B"));

            // MimeMessage encoding
        MimeMessage m = new MimeMessage((Session) null);
        m.setSubject("How to include £ pound symbol", "UTF-8");
        m.setContent("lalala", "text/plain");
        m.writeTo(System.out);
    }

并且输出是:

=?UTF-8?Q?How_to_include_=C2=A3_pound_symbol?=
=?UTF-8?B?SG93IHRvIGluY2x1ZGUgwqMgcG91bmQgc3ltYm9s?=

(. ..)

Message-ID: <21944831.01314352473121.JavaMail.HAC001ES@SE115179>
Subject: =?UTF-8?Q?How_to_include_=C2=A3_pound_symbol?=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

lalala

无论如何你总是可以使用:

String yourEncodedString = MimeUtility.encodeText(str, "UTF-8", "Q");
mimeMessage.setHeader("Subject", yourEncodedString);

Subject is a header. Headers use only ascii-7 so to encode none ascii-7 chars properly you should use proper encoding.

If the class you're using lets you indicate some encoding try with UTF-8.

mimeMessage.setSubject(yourSubject, "UTF-8");

If you're writing the headers by hand use any of this:

MimeUtility.encodeWord(yourSubject, "UTF-8", "B"); // base-64
MimeUtility.encodeWord(yourSubject, "UTF-8", "Q"); // quoted-printable

That's more or less what MimeMessage does in setSubject(str, encoding):

setHeader("Subject", MimeUtility.fold(9, MimeUtility.encodeText(subject, charset, null)));
// fold splits the value in several lines with no more than 72 chars

Sample

I've tried this:

public static void main(String[] args) throws Exception {
            // manual encoding
        System.out.println(MimeUtility.encodeText("How to include £ pound symbol", "UTF-8", "Q"));
        System.out.println(MimeUtility.encodeText("How to include £ pound symbol", "UTF-8", "B"));

            // MimeMessage encoding
        MimeMessage m = new MimeMessage((Session) null);
        m.setSubject("How to include £ pound symbol", "UTF-8");
        m.setContent("lalala", "text/plain");
        m.writeTo(System.out);
    }

and the output was:

=?UTF-8?Q?How_to_include_=C2=A3_pound_symbol?=
=?UTF-8?B?SG93IHRvIGluY2x1ZGUgwqMgcG91bmQgc3ltYm9s?=

(...)

Message-ID: <21944831.01314352473121.JavaMail.HAC001ES@SE115179>
Subject: =?UTF-8?Q?How_to_include_=C2=A3_pound_symbol?=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

lalala

Anyway you could always use:

String yourEncodedString = MimeUtility.encodeText(str, "UTF-8", "Q");
mimeMessage.setHeader("Subject", yourEncodedString);
梦纸 2024-12-08 16:11:11

将编码设置为 UTF-8..

msg.setContent(message,"text/html; charset=UTF-8");

Set your encoding to UTF-8..

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