Total Pageviews

2006/08/27

[Mail] commons-mail 亂碼問題

[常見問題1]sent出去的mail的中文標題變成亂碼

呼叫setCharset此方法,設定編碼方式為UTF-8
public void setCharset(java.lang.String newCharset)
http://jakarta.apache.org/commons/email/api-release/org/apache/commons/mail/Email.html#setCharset(java.lang.String)



[常見問題2]sent出去的mail的中文內文便成亂碼

呼叫setContent此方法,第一個參數給予訊息內容,第二個參數給予conent type,例如text/plain;charset=UTF-8
public void setContent(java.lang.Object aObject,
java.lang.String aContentType)
http://jakarta.apache.org/commons/email/api-release/org/apache/commons/mail/Email.html#setContent(java.lang.Object,%20java.lang.String)


[常見問題3]sent出去的附件中文檔名變成亂碼

在EmailAttachment的物件中的setName方法中,使用MimeUtility的encodeText方法
attachment.setName(MimeUtility.encodeText("中文訊息", "UTF-8", null));

http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeUtility.html

No comments: