JavaMail Encoding Setting
When i send the email in english, it has no problem.
But when i send in chinese, and i check the email in the outlook
express, i cannot see the chinese code, it needs me to set the
encoding in outlook express. For example, if it is chinese
traditional, i need choose chinese traditional to see it. If it is
simple chinese, it needs me to change to gb2312(simple chinese)
is there any method i can set when i send the email in my program, the
receiver can see it directly without his configuration?
Below is my code:
public boolean SendEmail(String EmailTo, String subject, String
Content, String MailServer, String arg_sEncoding)
{
boolean Rtr = false;
String mailhost=MailServer;
boolean debug =false;
String mailer = "msgsend";
String Emailfrom = EmailTo;
try
{
if(arg_sEncoding != null && !arg_sEncoding.equals(""))
{
arg_sEncoding = arg_sEncoding.trim();
}
else
{
arg_sEncoding = "en";
}
if(arg_sEncoding.toLowerCase().equals("en"))
{
arg_sEncoding = "ISO8859_1";
}else if(arg_sEncoding.toLowerCase().equals("zh_tw"))
{
arg_sEncoding = "MS950";
}else if(arg_sEncoding.toLowerCase().equals("zh_cn"))
{
arg_sEncoding = "GB2312";
}
if(!arg_sEncoding.toLowerCase().equals("ISO8859_1")
&& !arg_sEncoding.toLowerCase().equals("MS950")
&& !arg_sEncoding.toLowerCase().equals("GB2312"))
{
arg_sEncoding = "MS950";
}
Util.debugLog("In CQQPWebSendEmail -
arg_sEncoding:"+arg_sEncoding);
Properties props = System.getProperties();
// XXX - could use Session.getTransport() and
Transport.connect()
// XXX - assume we're using SMTP
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
// Get a Session object
Session session = Session.getInstance(props, null);
//debug= true;
if (debug)
session.setDebug(true);
// construct the message
Message msg = new MimeMessage(session);
if (Emailfrom != null)
msg.setFrom(new InternetAddress(Emailfrom, EmailTo,
arg_sEncoding));
else
msg.setFrom();
if(arg_sEncoding.toLowerCase().equals("ISO8859_1"))
{
msg.setContent(Content, "text/html; charset=ISO8859_1");
}else if(arg_sEncoding.toLowerCase().equals("MS950"))
{
msg.setContent(Content, "text/html; charset=MS950");
}else if(arg_sEncoding.toLowerCase().equals("GB2312"))
{
msg.setContent(Content, "text/html; charset=GB2312");
}
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(EmailTo, false));
/*if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));*/
msg.setSubject(subject);
// If the desired charset is known, you can use
// setText(text, charset)
//msg.setText(Content);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
// send the thing off
Transport.send(msg);
System.out.println("\nMail was sent successfully.");
Rtr = true;
}catch(Exception e) {
e.printStackTrace();
}
return Rtr;
}