javamail乱码问题(7)

2019-08-31 20:07

BASE64Encoder enc = new BASE64Encoder();//该类位于jre/lib/rt.jar中 //fds为FileDataSource实例

mbp.setFileName(\

2.接收邮件时,获取某些邮件发送程序发送的email地址,发送地址显示为乱码 解决办法:

对含有中文的发送地址,使用MimeUtility.decodeTex方法,对其他则把地址从ISO8859_1编码转换成gbk编码,见下例

public static String getFrom(Message msg){ String from=\ try{

if(msg.getFrom()[0]!=null)

from=msg.getFrom()[0].toString();

if(from.startsWith(\ from=MimeUtility.decodeText(from); }else{

from=StringUtil.toChinese(from); }

}catch(Exception e){ e.printStackTrace(); }

from=StringUtil.replaceStr(from,“<”,“<”);// replaceStr为字符串替换函数 from=StringUtil.replaceStr(from,\ return from; }

///////////////////StringUtil的toChinese方法////////////////////////// public static String toChinese(String strvalue){ try{

if(strvalue==null) return null; else{

strvalue = new String(strvalue.getBytes(\ return strvalue; }

}catch(Exception e){ return null; } }

3.接收邮件时,获取某个邮件的中文附件名,出现乱码

解决办法:

对于用base64编码过的中文,则采用base64解码,否则对附件名进行ISO8859_1到gbk的编码转换,例如:

String temp=part.getFileName();//part为Part实例 if((temp.startsWith(\ ||(temp.startsWith(\

temp=StringUtil.getFromBASE64(temp.substring(8,temp.indexOf(\}else{

temp=StringUtil.toChinese(temp);//该方法如前所叙 }

/////////////StringUtil的getFromBASE64方法///////// public static String getFromBASE64(String s) { if (s == null) return null;

BASE64Decoder decoder = new BASE64Decoder(); try {

byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }

乱码问题的调试步骤总结:

基本上在javamail中碰到的中文乱码问题就这么多了,如果你的程序出现了中文乱码,首先不要惊慌,可用多个其他的邮件发送或接收程序进行验证,看是在哪个环节出现了问题,然后再仔细对照原文和乱码,调用相应的编码解码方法就行了。

------------------------------------------------------------------------------------------------------------------

使用javamail发送html邮件比较复杂 package org.tatan.mail;

import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeUtility; import javax.mail.Session;

import javax.mail.MessagingException; import javax.mail.Transport; public class SendHtmlMail {

public static void sendMessage(String smtpHost, String from, String to,

String subject, String messageText)

throws MessagingException,java.io.UnsupportedEncodingException { // Step 1: Configure the mail session

System.out.println(\ java.util.Properties props = new java.util.Properties();

props.setProperty(\指定是否需要SMTP验证 props.setProperty(\指定SMTP服务器 props.put(\

Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(true);//是否在控制台显示debug信息 // Step 2: Construct the message

System.out.println(\from=\to=\ InternetAddress fromAddress = new InternetAddress(from); InternetAddress toAddress = new InternetAddress(to); MimeMessage testMessage = new MimeMessage(mailSession); testMessage.setFrom(fromAddress);

testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);

testMessage.setSentDate(new java.util.Date());

testMessage.setSubject(MimeUtility.encodeText(subject,\ testMessage.setContent(messageText, \ System.out.println(\ // Step 3: Now send the message

Transport transport = mailSession.getTransport(\ transport.connect(smtpHost, \

transport.sendMessage(testMessage, testMessage.getAllRecipients()); transport.close();

System.out.println(\ }

public static void main(String[] args) { String smtpHost = \

String from = \webmaster@mymail.com\ String to = \mfc42d@sohu.com\

String subject = \邮件测试\自动转码 StringBuffer theMessage = new StringBuffer();

theMessage.append(\这倒霉孩子\ theMessage.append(\

theMessage.append(\年年失望年年望\ try {

SendHtmlMail.sendMessage(smtpHost, from, to, subject, theMessage.toString()); }

catch (javax.mail.MessagingException exc) { exc.printStackTrace(); }

catch (java.io.UnsupportedEncodingException exc) { exc.printStackTrace(); } } }

邮件头(参见RFC822,RFC2047)只能包含US-ASCII字符。

邮件头中任何包含非US-ASCII字符的部分必须进行编码,使其只包含US-ASCII字符。 但是java mail可以根据JVM发送中文邮件自行编码,,用它自带的MimeUtility类的encodeText方法对中文信息进行编码也可以。 邮件正文必须有charset=gb2312否则为 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit 打开邮件为乱码,设置charset=gb2312后 Content-Type: text/html;charset=gb2312 Content-Transfer-Encoding: quoted-printable 它不能用MimeUtility里的方法来编码。

邮件正文的编码方式的信息是要放在Content-Transfer-Encoding这个邮件头参数中的, 而MimeUtility里面的方法是将编码方式的信息放在编码后的正文内容中。

所以如果你对正文也用MimeUtility进行处理,那么其他邮件程序就不会正常显示你编码的邮件,

因为其他邮件软件如outlook,foxmail只会根据Content-Transfer-Encoding这个里面的信息来对邮件正文进行解码。


javamail乱码问题(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:使用万维网练习

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: