当前位置: 代码迷 >> J2SE >> javamail发送邮件正文乱码…信头编码正确解决方法
  详细解决方案

javamail发送邮件正文乱码…信头编码正确解决方法

热度:77   发布时间:2016-04-24 14:53:57.0
javamail发送邮件正文乱码……信头编码正确
练习javamail写了一个发送邮件的程序,邮件标题采取以下设置可以正确显示中文:
=====
sun.misc.BASE64Encoder   enc   =   new   sun.misc.BASE64Encoder();       msg.setSubject( "=?GB2312?B? "+enc.encode(subject.getBytes())+ "?= ");
=====
但是正文部分采取以下代码却显示中文为乱码:
=====
msg.setText( "中文测试 ",   "GB2312 ");
msg.setHeader( "Content-Transfer-Encoding ",   "Base64 ");
-----
奇怪的是信头似乎是正确的:
Received:   from   zju-mchen   (unknown   [222.205.83.8])by   smtp14   (Coremail)   with   SMTP   id   wKjRD7D7ogbEf3ZG26ztAQ==.44476S2;
Mon,   18   Jun   2007   20:51:19   +0800   (CST)
From:   jerryshao1983@163.com
To:   enzymer@163.com
Message-ID:   <9031454.11182225057828.JavaMail.jerryshao@zju-mchen>
Subject:   =?GB2312?B?QklPT08gwLTQxQ==?=
MIME-Version:   1.0
Content-Type:   text/plain;   charset=GB2312
Content-Transfer-Encoding:   Base64
X-Coremail-Antispam:   1U3Yxn0WfASr-VFAUDIcSsGvfJTRUUUjkAFxVCF77xC6IxKo4
kEV4ylIxAIcVC2z280aVAFwI0_Gr0_Cr1lw4CEc2x0rVAKj4xxM4xvF2IEb7IF0Fy264kE
64k0F24lnxkEFVAIw20F6cxK64vIFxWl6VACY4xI67k04243AwC2zVAF1VAY17CE14v26r
1j6r15Mx02cVAKzwACY4xI67k04243AVC20s07MIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_
Gr1UMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4l4x8a6c8ajcxJMcIj6xIIjxv20xvE14v26r
1Y6r17MIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwAv7VC2z280aVAFwI0_Jr0_Gr1l
1I0EscIYIxCEI4klw4CSwwCY02Avz4vE14v_Xr1lc2IjII80xcxEwVAKI48JM7AC8VAFwI
0_Jr0_Gr1lb4IE77IF4wAFIxvE14AKwVWUJVWUGwAqx4xG64xvF2IEw4CE5I8CrVC2j2Wl
c7Ca8VAvwVA2a4k0FcxrM7k0a2IF6F4Un29KB7ZKAUJUUUUUnxnvy29KBjDU0xZFpf9x07
brxhLUUUUU129KBjDU=
Date:   Mon,   18   Jun   2007   20:51:21   +0800   (CST)

dGVzdCA/Pw==

==================
测试过,英文邮件完全可以正常显示,想来想去似乎应该是编码问题,但是……百思不得其解

谢谢大家帮忙。



------解决方案--------------------

msg.setText(content, encoding);
msg.setHeader( "Content-Type ", "text/plain; charset= " + encoding);


msg.setHeader( "Content-Transfer-Encoding ", "Base64 ");
用到这样的case也是有可能的。

其实mail的编码非常复杂。
lz可以尝试用utf8,看看如何。


------解决方案--------------------
用 "8859_1 "
  相关解决方案