[英]Java mail charset ISO-8859-2 not working
我遇到Java Mail API問題。
我可以成功發送郵件,但是一些特殊字符(來自ISO-8859-2語言,如捷克語,斯洛伐克語)不會在郵件中顯示。 即使在IDE輸出中它們也會損壞。
我究竟做錯了什么?
Message msg = new MimeMessage(session);
msg.setContent(message, "text/plain; charset=iso-8859-2")
我找到了解決方案,使用multipart。 這是代碼:
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
MimeMultipart multipart = new MimeMultipart();
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
MimeBodyPart tmpBp = new MimeBodyPart();
tmpBp.setContent(message,"text/plain; charset=utf-8");
multipart.addBodyPart(tmpBp);
msg.setContent(multipart);
Transport.send(msg);
msg.setContent(message,“text / plain; charset = UTF-8”);
而不是你給的charset?
而是使用UTF-8
作為字符集,並將IDE控制台配置為使用完全相同的字符集。 我不知道你正在使用哪個IDE,因為你沒有告訴它,但如果它是Eclipse,那么你可以通過Window > Preferences > General > Workspace > Text file encoding > Other > UTF-8來改變它。
如果這不能解決問題,那么問題出在其他地方。 也許您正在使用錯誤的編碼從文件中讀取消息。 為此,您需要使用InputStreamReader
,它將charset作為第二個構造函數參數。
您應該使用類MimeMessage
的setText
方法而不是setContent
/**
* Convenience method that sets the given String as this part's
* content, with a MIME type of "text/plain" and the specified
* charset. The given Unicode string will be charset-encoded
* using the specified charset. The charset is also used to set
* the "charset" parameter.
*
* @param text the text content to set
* @param charset the charset to use for the text
* @exception MessagingException if an error occurs
*/
public void setText(String text, String charset)
throws MessagingException {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.