gpt4 book ai didi

Java邮件: Setting Chinese Encoding

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:46 30 4
gpt4 key购买 nike

我通过 gmail 给自己发送了一封中文电子邮件,我成功收到了它,其中包含以下标题 Successful email header

当我通过 javax.mail 发送中文电子邮件时,它在我的邮件浏览器中显示如下 failed email

失败电子邮件的 header 如下: enter image description here

我的代码如下:

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

属性是:

  • 主机=smtp.gmail.com
  • mail.smtp.auth=true
  • mail.smtp.starttls.enable=true mail.smtp.host=smtp.gmail.com
  • mail.smtp.port=587 mail.mime.charset=UTF-8

注释:文本在发送之前没有被损坏。message.getSubject() 打印出干净的中文文本我猜我的编码方式有问题

编辑

这是调试输出:

DEBUG: setDebug: JavaMail version 1.5.5

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: need username and password for authentication

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false

220 smtp.gmail.com ESMTP s20sm18171725pfg.11 - gsmtp

DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

>

EHLO jake-yoga3.hitronhub.home

250-smtp.gmail.com at your service, [96.49.181.179]

250-SIZE 35882577

250-8BITMIME

250-STARTTLS

250-ENHANCEDSTATUSCODES

250-PIPELINING

250-CHUNKING

250 SMTPUTF8

DEBUG SMTP: Found extension "SIZE", arg "35882577"

DEBUG SMTP: Found extension "8BITMIME", arg ""

DEBUG SMTP: Found extension "STARTTLS", arg ""

DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""

DEBUG SMTP: Found extension "PIPELINING", arg ""

DEBUG SMTP: Found extension "CHUNKING", arg ""

DEBUG SMTP: Found extension "SMTPUTF8", arg ""

STARTTLS

220 2.0.0 Ready to start TLS

EHLO jake-yoga3.hitronhub.home

250-smtp.gmail.com at your service, [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: Found extension "SIZE", arg "35882577"

DEBUG SMTP: Found extension "8BITMIME", arg ""

DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"

DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""

DEBUG SMTP: Found extension "PIPELINING", arg ""

DEBUG SMTP: Found extension "CHUNKING", arg ""

DEBUG SMTP: Found extension "SMTPUTF8", arg ""

DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2

DEBUG SMTP: Using mechanism LOGIN

DEBUG SMTP: AUTH LOGIN command trace suppressed

DEBUG SMTP: AUTH LOGIN succeeded

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: Verified Addresses

DEBUG SMTP: JSTONE@I-KODA.COM

DATA

354 Go ahead s20sm18171725pfg.11 - gsmtp

From: iKoda Report

To: JSTONE@I-KODA.COM

Message-ID: <1926337998.1.1488002481716@jake-yoga3>

Subject: =?UTF-8?B?QnZ4Y2Igw6TCuMKtIMOkwrjCrSDDpMK4wq0gw6TCuMKtIMOkwrjCrQ==?=

MIME-Version: 1.0

Content-Type: multipart/mixed;

   boundary="----=_Part_0_1855484302.1488002481637"

>

------=_Part_0_1855484302.1488002481637

Content-Type: text/plain; charset=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: message successfully delivered to mail server

QUIT

221 2.0.0 closing connection s20sm18171725pfg.11 - gsmtp

[INFO] "jake.app" com.ikoda.service.utilities.EmailOut.send(EmailOut.java:256) 01:27:

>

Sent message successfully....

>

最佳答案

我已经发现问题了。问题不在 Email 类中,仅与 javamail 无关。(所以上面的代码是正确的)

问题在于从我的 Controller 中 Spring 的 MessageBundle 的热连线实例中提取文本。

我错误地使用了以下代码,以便我的日志记录类可以记录从消息包中提取的字符串。

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())

现在我的记录器刚刚收到 ???,但电子邮件发送得很好。

所以,保持简单愚蠢。

关于Java邮件: Setting Chinese Encoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451930/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com