strange characters(?) added to the end of my subje

2019-07-23 16:23发布

I have a problem with my java code sending email to users. There is some problem with the encoding of the email. When the email arrives to email account the subject line ($subject) has encoding problems as has strange characters(?) added to the end of my subject text.

The email message content itself is fine just the subject line(?) I have searched all over but cant find,after using Unicode and content type as text/html mail body have no problem with special character (ó) but same fix is not working for subject line.

I have a class that sends an email with javamail, with a text like this one in subject :

"Estimado Iván Escobedo:

The problem is that when the mail arrives to its destination, it arrives this way:

"Estimado Iv?n Escobedo:

All the á, é, í, ó, ú, etc special characters are replaced with ?.

What could be the problem and how to solve it?

1条回答
The star\"
2楼-- · 2019-07-23 17:24

You should use something like that to read the message properly:

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

Edit :

Sanjay found the solution.

In order to set properly the message before sending, use :

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.

Note that this method should be used to encode only "unstructured" RFC 822 headers.

查看更多
登录 后发表回答