UTF-8 charset doesn't work with javax.mail
For all e-mails
There are a couple of system properties related to mailing, that can probably simplify your code. I am talking about this specific property actually: "mail.mime.charset"
.
The
mail.mime.charset
System property can be used to specify the default MIME charset to use for encoded words and text parts that don't otherwise specify a charset. Normally, the default MIME charset is derived from the default Java charset, as specified in thefile.encoding
System property. Most applications will have no need to explicitly set the default MIME charset. In cases where the default MIME charset to be used for mail messages is different than the charset used for files stored on the system, this property should be set.
As you can read above, by default there is no value for the mail.mime.charset
and the file encoding (file.encoding
property) is used.
For a specific e-mail
However, if you want to specify a specific encoding for a specific e-mail, then you should probably use the 2 parameter setSubject(subject,charset)
and setText(text,charset)
methods.
If that doesn't work, then probably your input is already corrupted before it reached this point. In other words, you probably used the wrong encoding to collect your data.
Mime types are complicated
The setContent(content, "UTF-8")
(as other sources claim) will just not work. Just look at the signature of this method: setContent(Object content, String mimetype)
. Mime type and charset are 2 totally different things. Imho, you should really be using one of the setText(...)
methods with a charset parameter.
But if you persist in using a mimetype to set the charset setContent(content,mimetype)
, then use the correct format. (not just "UTF-8"
, but something like "text/plain; charset=UTF-8"
). But more importantly, be aware that every mime-type has its own way of handling charsets.
- As specified in RFC-2046 the default charset for
text/plain
isUS-ASCII
, but can be overruled with an additional charset parameter. - However, in RFC-6657 makes clear that the
text/xml
type determines the charset using the content of the message. The charset parameter will just be ignored here. - And in RFC-2854 is stated that
text/html
should really always specify a charset. But if you don't, then it will useISO-8859-1
(=Latin-1
).
Maybe You should provide also UTF-8 here
mimeMessage.setContent(message, "text/plain; charset=UTF-8");
You have to look at http://www.coderanch.com/t/274480/java/java/JavaMail-set-content-utf
After spending a lot of time on debugging, and searching the internet for a clue, I have found a solution to my problem.
It seems that whenever I sended data through a web request, my application didn't encode the characters with UTF-8 encoding. This meant that the data which was send from my contact form, which contained æ, ø and å characters, couldn't be handled correct by the character encoding.
The solution seemed to setup a Character Encoding Filter, in my Deployment Descriptor, which would encode all incoming request from the web to be with the character encoding UTF-8.
private void registerCharacterEncodingFilter(ServletContext servletContext) {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", encodingFilter);
characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
}
This filter sets the encoding to be UTF-8 and force the encoding to all requests comming at the url ' /* '.