我通过gmail向自己发送了一封中文电子邮件,我收到了以下标题
当我通过javax.mail发送中文电子邮件时,它在我的邮件浏览器中显示如下
我的代码如下:
public boolean send() throws TestReportingException, MessagingException
{
try
{
String encodingOptions = "text/plain; charset=UTF-8";
Authenticator authenticator = new Authenticator()
{
// override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
};
// Create the mail session
Session session = Session.getInstance(maileProperties, authenticator);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setHeader("Content-Type", encodingOptions);
// Set From: header field of the header.
mimeMessage.setFrom(new InternetAddress(from, fromName));
// Set To: header field of the header.
for (String s : toList)
{
if (null == s)
{
throw new TestReportingException("Email address is null");
}
mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(s));
}
for (String s : ccList)
{
mimeMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse(s));
}
// Set Subject: header field
mimeMessage.setSubject(subject,"UTF-8");
// Create the message part
//MimeBodyPart messageBodyPart = new MimeBodyPart();
// messageBodyPart.setContent(message, encodingOptions);
// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
mimeMessage.setHeader("Content-Type", encodingOptions);
// Now set the actual message
messageBodyPart.setText(message, "utf-8", "plain");
// Now set the actual message
//messageBodyPart.setText(message, "utf-8", "html");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
if (null != attachmentSource)
{
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(attachmentSource));
messageBodyPart.setFileName(attachmentSource.getName());
multipart.addBodyPart(messageBodyPart);
}
// Send the complete message parts
mimeMessage.setContent(multipart);
// Send message
//Transport.send(mimeMessage);
SSm.getLogger().debug("Subject: "+mimeMessage.getSubject());
Transport.send(mimeMessage);
SSm.getLogger().info("\n\nSent message successfully....");
clear();
return true;
}
catch (MessagingException mex)
{
SSm.getLogger().error(mex.getMessage());
throw mex;
}
catch (Exception e)
{
SSm.getLogger().error(e.getMessage(), e);
throw new TestReportingException(e.getMessage(), e);
}
}
属性是:
备注: 文本在发送之前未被破坏。 message.getSubject()打印出干净的中文文本 我猜测编码的方式有问题
修改
这是调试输出:
DEBUG:setDebug:JavaMail 1.5.5版
DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP:需要用户名和密码进行身份验证
DEBUG SMTP:useEhlo true,useAuth true
DEBUG SMTP:尝试连接到主机" smtp.gmail.com",端口587,isSSL false
220 smtp.gmail.com ESMTP s20sm18171725pfg.11 - gsmtp
DEBUG SMTP:连接到主机" smtp.gmail.com",端口:587
>
EHLO jake-yoga3.hitronhub.home
250-smtp.gmail.com为您服务,[96.49.181.179]
250-SIZE 35882577
250-8BITMIME
250 STARTTLS
250 ENHANCEDSTATUSCODES
250 PIPELINING
250 CHUNKING
250 SMTPUTF8
DEBUG SMTP:找到扩展名" SIZE",arg" 35882577"
DEBUG SMTP:找到扩展名" 8BITMIME",arg""
DEBUG SMTP:找到扩展名" STARTTLS",arg""
DEBUG SMTP:找到扩展名" ENHANCEDSTATUSCODES",arg""
DEBUG SMTP:找到扩展名" PIPELINING",arg""
DEBUG SMTP:找到扩展名" CHUNKING",arg""
DEBUG SMTP:找到扩展名" SMTPUTF8",arg""
STARTTLS
220 2.0.0准备启动TLS
EHLO jake-yoga3.hitronhub.home
250-smtp.gmail.com为您服务,[96.49.181.179]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250 ENHANCEDSTATUSCODES
250 PIPELINING
250 CHUNKING
250 SMTPUTF8
DEBUG SMTP:找到扩展名" SIZE",arg" 35882577"
DEBUG SMTP:找到扩展名" 8BITMIME",arg""
DEBUG SMTP:找到扩展名" AUTH",arg"登录平台XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP:找到扩展名" ENHANCEDSTATUSCODES",arg""
DEBUG SMTP:找到扩展名" PIPELINING",arg""
DEBUG SMTP:找到扩展名" CHUNKING",arg""
DEBUG SMTP:找到扩展名" SMTPUTF8",arg""
DEBUG SMTP:尝试使用机制进行身份验证:LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP:使用机制LOGIN
DEBUG SMTP:AUTH LOGIN命令跟踪被抑制
DEBUG SMTP:AUTH LOGIN成功
DEBUG SMTP:use8bit false
MAIL FROM:
250 2.1.0 OK s20sm18171725pfg.11 - gsmtp
RCPT TO:
250 2.1.5 OK s20sm18171725pfg.11 - gsmtp
DEBUG SMTP:已验证的地址
DEBUG SMTP:JSTONE@I-KODA.COM
DATA
354来吧s20sm18171725pfg.11 - gsmtp
来自:iKoda报告
致:JSTONE@I-KODA.COM
消息ID:< 1926337998.1.1488002481716@jake-yoga3>
主题:=?UTF-8?B?QnZ4Y2Igw6TCuMKtIMOkwrjCrSDDpMK4wq0gw6TCuMKtIMOkwrjCrQ ==?=
MIME-Version:1.0
内容类型:multipart / mixed;
boundary="----=_Part_0_1855484302.1488002481637"
>
------ = _ Part_0_1855484302.1488002481637
Content-Type:text / plain;字符集= UTF-8
Content-Transfer-Encoding:base64
>
RGVhciBaeGN2LA0KDQpCdnhjYiDDpMK4wq0gw6TCuMKtIMOkwrjCrSDDpMK4wq0gw6TCuMKtw6TC
uMKtIMOkwrjCrSDDpMK4wq0gw6TCuMKtIMOkwrjCrWh0dHBzOi8vd3d3Lmkta29kYS5jb20vZGVs
aXZlcnkvZHNmcj91ZnQ9MTAxMjc3MCZjPTEwMTI3NjTDpMK4wq0gw6TCuMKtIMOkwrjCrSDDpMK4
wq0gw6TCuMKt
------ = _ Part_0_1855484302.1488002481637 -
250 2.0.0 OK 1488002489 s20sm18171725pfg.11 - gsmtp
DEBUG SMTP:邮件已成功发送到邮件服务器
QUIT
221 2.0.0关闭连接s20sm18171725pfg.11 - gsmtp
[INFO]" jake.app" com.ikoda.service.utilities.EmailOut.send(EmailOut.java:256)01:27:
>
成功发送消息....
>
答案 0 :(得分:0)
我发现了这个问题。 问题不在电子邮件类中,只与javamail相关。 (所以上面的代码是正确的)
问题在于从我的Controller中的Spring的MessageBundle的hotwired实例中提取文本。
我不正确地使用了以下代码,以便我的日志记录类可以记录从消息包中提取的字符串。
byte[] barray =messageSource.getMessage(code, null, LocaleContextHolder.getLocale()).getBytes(Charset.forName("UTF-8"));
String s = new String(barray);
Log4j可以读取主题中的字符串和消息(由StringBuilder构建),这使我相信字符串是正确的UTF-8。但是,javax.mail在传输中出现乱码。
我应该做的是:
messageSource.getMessage(code, null, LocaleContextHolder.getLocale())
现在我的记录器刚刚获得???,但电子邮件发送得很好。
所以,保持简单愚蠢。