Reputation: 155
I am having problem with Java Mail API.
I can successfully send mail, but some special characters (from ISO-8859-2 languages like czech, slovak) are not shown in mail. They are damaged even in IDE output.
What am I doing wrong?
Message msg = new MimeMessage(session);
msg.setContent(message, "text/plain; charset=iso-8859-2")
Upvotes: 4
Views: 7623
Reputation: 155
I found solution, using multipart. here is code :
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
MimeMultipart multipart = new MimeMultipart();
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
MimeBodyPart tmpBp = new MimeBodyPart();
tmpBp.setContent(message,"text/plain; charset=utf-8");
multipart.addBodyPart(tmpBp);
msg.setContent(multipart);
Transport.send(msg);
Upvotes: 2
Reputation: 21
msg.setContent(message, "text/plain; charset=UTF-8");
instead of the charset you've given?
Upvotes: 2
Reputation: 531
You should use the setText
method from the class MimeMessage
instead of setContent
/**
* Convenience method that sets the given String as this part's
* content, with a MIME type of "text/plain" and the specified
* charset. The given Unicode string will be charset-encoded
* using the specified charset. The charset is also used to set
* the "charset" parameter.
*
* @param text the text content to set
* @param charset the charset to use for the text
* @exception MessagingException if an error occurs
*/
public void setText(String text, String charset)
throws MessagingException {
Upvotes: 0
Reputation: 1108642
Rather use UTF-8
as charset and configure your IDE console to use the very same charset as well. I don't know which IDE you're using as you didn't tell about it, but if it were Eclipse, then you can change it by Window > Preferences > General > Workspace > Text file encoding > Other > UTF-8.
If that doesn't fix the problem, then the problem lies somewhere else. Maybe you're reading the message from a file using the wrong encoding. For that you need to use InputStreamReader
which takes the charset as 2nd constructor argument.
Upvotes: 0