I have used Java Mail API, for sending emails. I am using a contact formular to send the input, which has to be send to a specific email.
The email is send without problems, though I am a danish guy, and I am therefore in need of three danish characters which is 'æ', 'ø' and 'å', in the subject and the email text.
I have therefore seen that I can use UTF-8 character encoding, to provide these characters, but when my mail is send I only see some strange letters - 'ã¦', 'ã¸' and 'ã¥' - instead of the danish letters - 'æ', 'ø' and 'å'.
My method to send the email is looking like this:
public void sendEmail(String name, String fromEmail, String subject, String message) throws AddressException, MessagingException, UnsupportedEncodingException, SendFailedException { //Set Mail properties Properties props = System.getProperties(); props.setProperty("mail.smtp.starttls.enable", "true"); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("my_username", "my_password"); } }); //Create the email with variable input MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setHeader("Content-Type", "text/plain; charset=UTF-8"); mimeMessage.setFrom(new InternetAddress(fromEmail, name)); mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("my_email")); mimeMessage.setSubject(subject, "utf-8"); mimeMessage.setContent(message, "text/plain"); //Send the email Transport.send(mimeMessage); }
Please help me find out how I can correct this 'error'.
UTF-8 supports any unicode character, which pragmatically means any natural language (Coptic, Sinhala, Phonecian, Cherokee etc), as well as many non-spoken languages (Music notation, mathematical symbols, APL). The stated objective of the Unicode consortium is to encompass all communications.
The javax. mail. internet package defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP.
Definition and Usage The charset attribute specifies the character encoding for the HTML document. The HTML5 specification encourages web developers to use the UTF-8 character set, which covers almost all of the characters and symbols in the world!
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.
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.
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.
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.
text/plain
is US-ASCII
, but can be overruled with an additional charset parameter.text/xml
type determines the charset using the content of the message. The charset parameter will just be ignored here. text/html
should really always specify a charset. But if you don't, then it will use ISO-8859-1
(=Latin-1
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With