UTF-8 字符集不适用于 javax.mail [英] UTF-8 charset doesn't work with javax.mail
问题描述
我使用 Java Mail API 来发送电子邮件.我正在使用联系人公式发送输入,该输入必须发送到特定的电子邮件.
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.
因此,我看到我可以使用 UTF-8 字符编码来提供这些字符,但是当我发送邮件时,我只看到一些奇怪的字母 - 'ã|'、'ã¸' 和 'ã¥' -而不是丹麦字母 - 'æ'、'ø' 和 'å'.
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'.
推荐答案
对于所有电子邮件
有几个 系统属性与邮寄相关,这可能可以简化您的代码.我实际上是在谈论这个特定的属性:"mail.mime.charset"
.
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"
.
mail.mime.charset
系统属性可用于指定默认 MIME 字符集,用于编码的单词和文本部分不要以其他方式指定字符集.通常,默认 MIME 字符集源自默认 Java 字符集,如 file.encoding
系统属性中指定的那样.大多数应用程序不需要显式设置默认 MIME 字符集.如果用于邮件消息的默认 MIME 字符集与用于存储在系统上的文件的字符集不同,则应设置此属性.
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.
如上所述,默认情况下 mail.mime.charset
没有值,并且使用文件编码(file.encoding
属性).
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.
但是,如果您想为特定的电子邮件指定特定的编码,那么您可能应该使用 2 个参数 setSubject(subject,charset)
和 setText(text,charset))
方法.
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.
如果这不起作用,那么可能您的输入在到达这一点之前已经损坏.换句话说,您可能使用了错误的编码来收集数据.
setContent(content, UTF-8")
(正如其他来源声称的那样)将不起作用.看看这个方法的签名:setContent(Object content, String mimetype)
.Mime 类型和字符集是两种完全不同的东西. 恕我直言,您确实应该使用带有字符集参数的 setText(...)
方法之一.
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.
但是如果您坚持使用 mimetype 来设置字符集 setContent(content,mimetype)
,那么请使用正确的格式.(不仅仅是UTF-8"
,而是类似于text/plain; charset=UTF-8"
).但更重要的是,请注意,每种 mime 类型都有自己处理字符集的方式.
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.
- 按照 RFC-2046 中的规定,
text/的默认字符集普通
是US-ASCII
,但可以用一个额外的字符集参数来否决. - 但是,在 RFC-6657 中明确指出
text/xml
类型使用消息的内容确定字符集.这里的字符集参数将被忽略. - 在 RFC-2854 中声明
text/html
真的应该总是指定一个字符集.但如果你不这样做,那么它将使用ISO-8859-1
(=Latin-1
).
- 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
).
这篇关于UTF-8 字符集不适用于 javax.mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!