Click to See Complete Forum and Search --> : javamail and java + international letters


thisObject
05-15-2006, 11:25 AM
Hello,

I have a problem setting up subject of an email when sending out an email using JavaMail in Russian letters.

Code:

MimeMessage msg = new MimeMessage(session);
msg.setSubject("some Russian letters","some encoding");


I've tried different encodings but nothing works. I get question marks no matter what.

Has anyone ever had to deal with this? What should be the correct encoding?
OS that I am using is Fedora Core 5.

In general, if I wan to create a string that contains Russian characters such as:
String s = "Some words in Russian";
Later, I want to treat this string as any other string in my program (pass it to a method and so on).

Which encoding should I use and how should I handle this string?

Thank you.

thisObject
05-15-2006, 09:57 PM
Here is some code that I have:


private SendEmail sendEmail = new SendEmail();
sendEmail.sendSSLMessage(email, "Some Russian letters here", "XXX@gmail.com");


Code for sendSSLMessage



public void sendSSLMessage(String recipient, String message, String from) throws MessagingException
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("login", "password");
}
}
);

session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
InternetAddress internetAddress = new InternetAddress();
internetAddress.setAddress(from);
msg.setFrom(internetAddress);
msg.setRecipients(Message.RecipientType.TO, recipient);

try
{
msg.setSubject("Some Russian letters here", "Cp1251");
msg.setText(message, "windows-cp1251");
msg.setContent(message, "text/html; charset=koi8-r");
}
catch( Exception e )
{
e.printStackTrace();
}

Transport.send(msg);
}



I think everything gets messed up at the end of the code (last try block). I can't figure out which encoding to use so the end user gets Russian characters in email.


Thank you.