JavaMail Encoding Setting

M

mike

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Javamail smtp port number 3
Javamail - error SMTP 554 1
Email not working 0
java mail api 3
Problems sending email javamail 3
java mail replyTo problem 2
javamail send error 5
Javamail problem 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top