java 使用 JavaMail 发送包含 Unicode 字符的邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:41:38  来源:igfitidea点击:

Using JavaMail to send a mail containing Unicode characters

javaunicodejavamail

提问by NoozNooz42

I'm successfully sending emails through GMail's SMTP servers using the following piece of code:

我使用以下代码成功通过 GMail 的 SMTP 服务器发送电子邮件:

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.ssl", "true");                 
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.timeout", "5000");             
        props.put("mail.smtp.connectiontimeout", "5000"); 

        // Do NOT use Session.getDefaultInstance but Session.getInstance
        // See: http://forums.sun.com/thread.jspa?threadID=5301696
        final Session session = Session.getInstance( props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication( USER, PWD );
                    }
                });

        try {
            final Message message = new MimeMessage(session);
            message.setFrom( new InternetAddress( USER ) );
            message.setRecipients( Message.RecipientType.TO, InternetAddress.parse( TO ) );
            message.setSubject( emailSubject );
            message.setText( emailContent );
            Transport.send(message);
            emailSent = true;
        } catch ( final MessagingException e ) {
            e.printStackTrace();
        }

where emailContentis a Stringthat does contain Unicode characters (like the euro symbol).

其中emailContent是一个包含 Unicode 字符(如欧元符号)的字符串

When the email arrives (in another GMail account), the euro symbol has been converted to the ASCII '?' question mark.

当电子邮件到达时(在另一个 GMail 帐户中),欧元符号已转换为 ASCII '?' 问号。

I don't know much about emails: can email use any character encoding?

我不太了解电子邮件:电子邮件可以使用任何字符编码吗?

What should I modify in the code above so that an encoding allowing Unicode characters is used?

我应该在上面的代码中修改什么,以便使用允许 Unicode 字符的编码?

回答by NoozNooz42

Answering my own question: you need to use the setHeadermethod from the Messageclass, like this (the following has been tried and it is working):

回答我自己的问题:您需要使用Message类中的setHeader方法,如下所示(已尝试以下方法并且它正在工作):

message.setHeader("Content-Type", "text/plain; charset=UTF-8");

回答by bmargulies

you will need MIME headers specifying the content type to tell it that you want to send email in UTF-8.

您将需要指定内容类型的 MIME 标头,以告诉它您想以 UTF-8 格式发送电子邮件。

Use a MimeMessage and call setText with two arguments, passing in the charset.

使用 MimeMessage 并使用两个参数调用 setText,传入字符集。