所谓众口难调,但我相信总有一款适合你
1,在设置邮件主题前需要将主题字串的字节编码为BASE64格式,并添加编码头,示例代码如下:
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
msg.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");
2,((MimeMessage)message).setSubject(subject, "GBK");
3,message.setSubject(new String(ttitle.getBytes("GB2312"),"ISO8859-1"));
4,mailsubject = MimeUtility.encodeText(new String(mailsubject.getBytes(), "GB2312"), "GB2312", "B");
========================================================================
解决内容乱码可以这样:
//设置邮件文字内容部分
MimeBodyPart textBodyPart = new MimeBodyPart();
//这里转换文字内容到gb2312格式。
textBodyPart.setContent(content, "text/html;charset=\"GB2312\"");
//设置邮件附件加载器
MimeBodyPart fileBodyPart = new MimeBodyPart();
javax.activation.FileDataSource fds = new FileDataSource(path);
//要发送的附件
Multipart container = new MimeMultipart();
File f = new File(path);
if (f.exists())
{
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName("=?GBK?B?" +
enc.encode( (fds.getName()).getBytes()) +
"?=");//这里解决了附件名乱码问题
container.addBodyPart(fileBodyPart);//加载附件到邮件容器
}
container.addBodyPart(textBodyPart);
message.setContent(container);//加载容器到消息类
其中的enc实例我是这样定义的:
Base64Converter enc = new Base64Converter();
这里面的Base64Converter类自己可以编写,如下:
public class Base64Converter
{
public static final char[] alphabet = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/'}; // 56 to 63
public static String encode(String s)
{
return encode(s.getBytes());
}
public static String encode(byte[] octetString)
{
int bits24;
int bits6;
char[] out
= new char[ ( (octetString.length - 1) / 3 + 1) * 4];
int outIndex = 0;
int i = 0;
while ( (i + 3) <= octetString.length)
{
bits24 = (octetString[i++] & 0xFF) << 16;
bits24 |= (octetString[i++] & 0xFF) << 8;
bits24 |= (octetString[i++] & 0xFF) << 0;
bits6 = (bits24 & 0x00FC0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x0003F000) >> 12;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x00000FC0) >> 6;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x0000003F);
out[outIndex++] = alphabet[bits6];
}
if (octetString.length - i == 2)
{
bits24 = (octetString[i] & 0xFF) << 16;
bits24 |= (octetString[i + 1] & 0xFF) << 8;
bits6 = (bits24 & 0x00FC0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x0003F000) >> 12;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x00000FC0) >> 6;
out[outIndex++] = alphabet[bits6];
out[outIndex++] = '=';
}
else if (octetString.length - i == 1)
{
bits24 = (octetString[i] & 0xFF) << 16;
bits6 = (bits24 & 0x00FC0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x0003F000) >> 12;
out[outIndex++] = alphabet[bits6];
out[outIndex++] = '=';
out[outIndex++] = '=';
}
return new String(out);
}
}