javamail中文问题
原来在windows中文系统,发送邮件正常,现在平台转移到linux 英文版下,发送的邮件内容出现中文乱码,代码如下:
public static void sendmail(Mail mail){
System.out.println("toname="+mail.getToname());
System.out.println("subject="+mail.getSubject());
System.out.println("context="+mail.getContext());
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "aaa@163.com"; //发邮件的出发地(发件人的信箱)
String to = mail.getToname(); //发邮件的目的地(收件人信箱)
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator("aaa", "aaa");
Session session = Session.getDefaultInstance(props, myauth);
//session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
try{
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject(mail.getSubject());
// Set the content
String context1=new String(mail.getContext().getBytes("GB2312"),"8859_1");
message.setContent(context1, "text/html");
message.saveChanges();
Transport.send(message);
}catch(Exception e){
System.out.println("send mail error!");
}
}
不改变linx系统编码的情况下,代码是怎么做修改???
------解决方案--------------------
String context1=new String(mail.getContext().getBytes("GB2312"),"8859_1");
这里转码的问题,windows下是正确的,linux下换一下别的试一下!
------解决方案--------------------
MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B")
------解决方案--------------------