亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1388 | 回復(fù): 0
打印 上一主題 下一主題

我使用javamail處理中文遇到的一些問題及解決方法 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2004-11-27 13:54 |只看該作者 |倒序?yàn)g覽
先從最棘手的來吧
1.如何正確顯示郵件內(nèi)容為中文明碼而且Content-Type里沒有指定charset的郵件
   據(jù)我所知,新浪發(fā)出的郵件都是這樣的,我的youmail里無法正確顯示,為亂碼.后來分析內(nèi)容,才知道是他們的郵件里沒有指定charset..
郵件內(nèi)容部分如:
  1. From: hrbtvu_ly <foo@sina.com>;
  2. To: foo@mail.foo.com
  3. Subject: 你好啊
  4. MIME-Version: 1.0
  5. Date: Thu, 11 Nov 2004 12:51:30 +0800
  6. X-Mailer: SinaMail 3.0Beta (FireToad)
  7. X-Priority: 3
  8. X-Antivirus-MYDOMAIN-1.22-st-qms: added fake Content-Type header
  9. Content-Type: text/plain

  10. 你好啊
復(fù)制代碼

可以看出連標(biāo)題都是中文明碼的,這個我后面講到如何處理.
我的解決方法是:修改com.sun.mail.handlers.text_plain類的getCharset(),修改成:
   
  1. private String getCharset(String s)
  2.     {
  3.         try
  4.         {
  5.             ContentType contenttype = new ContentType(s);
  6.             String s1 = contenttype.getParameter("charset");
  7.             if(s1 == null)
  8.                 s1 = "gb2312";
  9.             return MimeUtility.javaCharset(s1);
  10.         }
  11.         catch(Exception _ex)
  12.         {
  13.             return "gb2312";
  14.         }
  15.     }
復(fù)制代碼

這是我認(rèn)為最簡單的辦法了.

2.Subject或其他字段為中文明碼的處理
這里我提供我寫的一個方法,能夠統(tǒng)一處理編碼過的或者明文的字符串:
   

  1.     /**
  2.      * 返回解碼后的字符串
  3.    */
  4.     public static String getDecodeText(String eword) {
  5.         if (eword == null) return null;
  6.         try {
  7.             Pattern p = Pattern.compile("=\\?.+\\?=");
  8.             Matcher m = p.matcher(eword);
  9.             StringBuffer sb = new StringBuffer();
  10.             boolean result = m.find();
  11.             if (result) {
  12.                 while (result) {
  13.                     m.appendReplacement(sb, MimeUtility.decodeText(m.group()));
  14.                     result = m.find();
  15.                 }
  16.                 m.appendTail(sb);
  17.                 return sb.toString();
  18.             } else {
  19.                 String decodevalue = new String(eword.getBytes("ISO8859-1"), "GBK");
  20.                 return decodevalue;
  21.             }
  22.         } catch (Exception e) {
  23.             e.printStackTrace();
  24.             return eword;
  25.         }
  26.     }
復(fù)制代碼


3.發(fā)送郵件中中文的處理
   郵件標(biāo)題: newmsg.setSubject(subject, "GBK";
   附件:
  BASE64Encoder enc = new BASE64Encoder();
   mbp2.setFileName("=?GBK?B?" + enc.encode((new String(ai.getFileName()).getBytes("GBK")) + "?=";
   這只是給出一個方法,具體你還要自己改一改
  內(nèi)容:msg.setContent(body, "text/plain;charset=GBK";
  主要原則是處處指定charset

今天先說這么多了,有問題大家mailto:topmint@hit.edu.cn,或qq:100949955
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP