utf 8字符集不能与javax邮件一起使用 [英] utf 8 charset doesn't work with javax mail

查看:127
本文介绍了utf 8字符集不能与javax邮件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Javax Mail API来发送电子邮件。我使用联系人formular发送输入,必须发送到一个特定的电子邮件。

I have used Javax 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);
}

请帮助我找到如何纠正这个错误。 p>

Please help me find out how I can correct this 'error'.

推荐答案

对于所有电子邮件



href =http://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html =nofollow>与邮件相关的系统属性,可能简化您的代码。我正在谈论这个具体的属性: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 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.

正如你可以看到的,默认情况下没有值 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

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类型和字符集是两个完全不同的东西。 Imho,你应该真正使用的一个 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 / plain US-ASCII ,但可以通过额外的charset参数来覆盖。

  • 但是,在 RFC-6657 中, text / xml 类型使用消息的内容确定字符集。 此处的charset参数将被忽略。

  • RFC-2854 表示 text / html 应该总是指定一个字符集。但如果没有,那么它将使用 ISO-8859-1 (= Latin-1 )。 / li>
  • 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邮件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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