Using the code below i can send an email written in non-english and although the subject appears correctly the body appears as gibberish.
Any ideas? Thank youpublic void postMail(String recipient, String subject, String message, String from) throws MessagingException, UnsupportedEncodingException {
//Set the host smtp address
Properties props = new Properti开发者_StackOverflow社区es();
props.put("mail.smtp.host", "mail.infodim.gr");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress addressTo=new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
Try:
msg.setContent(message, "text/plain; charset=UTF-8");
Edit Changed to text/plain
.
Instead of
msg.setContent(message, "text/plain");
I would write
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(message, "text/plain; charset=ISO-8859-7");
mp.addBodyPart(mbp);
msg.setContent(mp);
I guessed ISO-8859-7
from your name because this charset is for Greek, but maybe you can choose it more properly. Or maybe also UTF-8
works for your case.
If nothing else helps, try changing an encoding of your source files (including .java files) to UTF8. In Eclipse it is done via Window -> Preferences -> General -> Workspace : Text file encoding I had CP1252 as a default for my text files.
I am getting my text from .properties files. Changing them to UTF8 didn't help. This is insane, but switching my .java files to UTF8 solved my issue!
精彩评论