gpt4 book ai didi

java - 奇怪的字符(?)添加到我的主题文本的末尾

转载 作者:行者123 更新时间:2023-11-30 09:40:31 24 4
gpt4 key购买 nike

我的 java 代码向用户发送电子邮件时出现问题。电子邮件的编码存在一些问题。当电子邮件到达电子邮件帐户时,主题行 ($subject) 存在编码问题,因为在我的主题文本末尾添加了奇怪的字符 (?)。

电子邮件内容本身很好,只是主题行(?)我搜索了所有但找不到,在使用 Unicode 和内容类型作为文本/html 邮件正文后,特殊字符没有问题(ó) 但同样的修复不适用于主题行。

我有一个用 javamail 发送电子邮件的类,其中的文本如下这个主题:

"Estimado Iván Escobedo:

问题是当邮件到达目的地时,它以这种方式到达:

"Estimado Iv?n Escobedo:

所有的á, é, í, ó, ú,等特殊字符替换为 ?

可能是什么问题以及如何解决?

最佳答案

你应该使用类似的东西来正确阅读消息:

TextMessage txtMessage = (TextMessage)message;
ByteArrayInputStream bais = new ByteArrayInputStream(txtMessage.getText().getBytes ("ISO-8859-15"))

编辑:

Sanjay 找到了解决方案。

为了在发送前正确设置消息,使用:

MimeUtility.encodeText(SubjectText, "ISO-8859-15", "Q")

encodeText :

Encode a RFC 822 "text" token into mail-safe form as per RFC 2047.

The given Unicode string is examined for non US-ASCII characters. If the string contains only US-ASCII characters, it is returned as-is. If the string contains non US-ASCII characters, it is first character-encoded using the specified charset, then transfer-encoded using either the B or Q encoding. The resulting bytes are then returned as a Unicode string containing only ASCII characters.

请注意,此方法应该仅用于对“非结构化”RFC 822 header 进行编码。

关于java - 奇怪的字符(?)添加到我的主题文本的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9378084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com