当前位置: 技术问答>java相关
java发送邮件的问题!
来源: 互联网 发布时间:2015-04-21
本文导语: 我使用下列代码发送邮件,但是中文出来的都是乱码,向各位大虾请教,怎样才能使中文不以乱码出现。 我用MimeMessage.setContentLanguage()不知道该怎样传递参数 import javax.mail.*; import javax.mail.internet.*; import java.uti...
我使用下列代码发送邮件,但是中文出来的都是乱码,向各位大虾请教,怎样才能使中文不以乱码出现。
我用MimeMessage.setContentLanguage()不知道该怎样传递参数
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Assimilator {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.host", "192.168.0.18");
try {
Session mailConnection = Session.getInstance(props, null);
Address bill = new InternetAddress("jike", "yyh");
Address elliotte = new InternetAddress("yangyh@beyondsoft.com");
Message msg = new MimeMessage(mailConnection);
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO, elliotte);
msg.setSubject("You must comply.");
String[] lang={"i-zh-CN"};
((MimeMessage)msg).setContentLanguage(lang);
msg.setContent("Resistance is futile. 操",
"text/html");
Transport.send(((MimeMessage)msg));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我用MimeMessage.setContentLanguage()不知道该怎样传递参数
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Assimilator {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.host", "192.168.0.18");
try {
Session mailConnection = Session.getInstance(props, null);
Address bill = new InternetAddress("jike", "yyh");
Address elliotte = new InternetAddress("yangyh@beyondsoft.com");
Message msg = new MimeMessage(mailConnection);
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO, elliotte);
msg.setSubject("You must comply.");
String[] lang={"i-zh-CN"};
((MimeMessage)msg).setContentLanguage(lang);
msg.setContent("Resistance is futile. 操",
"text/html");
Transport.send(((MimeMessage)msg));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
msg.setContent("Resistance is futile. 操",
"text/html;charset=gb2312");
"text/html;charset=gb2312");
|
尝试用不同的编码
msg.setContent(new String("Resistance is futile. 操".getBytes("iso-8859-1","GB2312")),
"text/html");
msg.setContent(new String("Resistance is futile. 操".getBytes("iso-8859-1","GB2312")),
"text/html");