UTF-8字符集不适用于javax.mail [英] UTF-8 charset doesn't work with javax.mail

查看:251
本文介绍了UTF-8字符集不适用于javax.mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Java Mail API 发送电子邮件.我正在使用联系人公式器来发送输入,必须将其发送到特定的电子邮件.

尽管我是丹麦人,但电子邮件发送没有问题,因此,在主题和电子邮件文本中,我需要三个丹麦字符,分别是æ",ø"和å"./p>

因此,我看到可以使用UTF-8字符编码来提供这些字符,但是在发送邮件时,我只会看到一些奇怪的字母-'ã¦','ã¸'和'ã¥'-而不是丹麦字母-æ",ø"和å".

我发送电子邮件的方法如下:

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);
}

请帮助我找出如何纠正此错误".

解决方案

对于所有电子邮件

有几个与邮件相关的系统属性,这可能会简化您的代码.我实际上是在谈论这个特定的属性:"mail.mime.charset".

mail.mime.charset 系统属性可用于指定默认的MIME字符集,用于默认情况下未指定字符集的编码单词和文本部分.通常,默认的MIME字符集是从file.encoding系统属性中指定的默认Java字符集派生的.大多数应用程序无需显式设置默认的MIME字符集.如果用于邮件的默认MIME字符集与用于系统上存储的文件的字符集不同,则应设置此属性.

如上所述,默认情况下,mail.mime.charset没有任何值,并且使用文件编码(file.encoding属性).

对于特定电子邮件

但是,如果要为特定的电子邮件指定特定的编码,则可能应该使用2个参数setSubject(subject,charset)setText(text,charset)方法.

如果这不起作用,则可能是您的输入在此之前已经损坏.换句话说,您可能使用了错误的编码来收集数据.

MIME类型很复杂

setContent(content, "UTF-8")(如其他消息来源所述)将无法正常工作.只需查看此方法的签名:setContent(Object content, String mimetype). Mime类型和字符集是两个完全不同的东西.您确实应该使用具有字符集参数的setText(...)方法之一.

但是,如果您坚持使用模仿类型来设置字符集setContent(content,mimetype),请使用正确的格式. (不仅是"UTF-8",还包括"text/plain; charset=UTF-8"之类的东西).但更重要的是,请注意,每种mime类型都有其自己的处理字符集的方式.

  • 按照 RFC-2046 中的规定,text/plain的默认字符集为US-ASCII,但可以用一个附加的字符集参数.
  • 但是,在 RFC-6657 中明确指出,text/xml类型使用消息的内容来确定字符集. . 此处的charset参数将被忽略.
  • RFC-2854 中指出,text/html实际上应该始终指定一个字符集.但是,如果不这样做,它将使用ISO-8859-1(= Latin-1).

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'.

解决方案

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 the file.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 is US-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 use ISO-8859-1 (=Latin-1).

这篇关于UTF-8字符集不适用于javax.mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆