javax邮件:UTF-8编码问题

4

我看到了很多关于这个问题的提问,但是没有一个能解决我的问题。

我有一封带有pdf附件的中文邮件。 所有的文本都是有效的UTF-8,直到它被包含在MultiPart邮件中。

问题: 当邮件到达收件人时,邮件中的文本是乱码字符。邮件头显示它没有正确编码。

下面我会提供我的代码和邮件头:

编辑: 我已经解决了属性问题,但我的错误仍然存在

邮件头信息

> eturn-Path: <jstone@eee.com>
>Received: from jake-yoga3.hitronhub.home (S01061cabc083fd23.vc.shawcable.net. [96.49.181.179])
>        by smtp.gmail.com with ESMTPSA id n189sm16430794pfn.108.2017.02.24.11.29.53
>        for <JSTONE@eee.com>
>        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
>        Fri, 24 Feb 2017 11:29:54 -0800 (PST)
>Date: Fri, 24 Feb 2017 11:29:54 -0800 (PST)
>From: iKoda Report <jstone@eee.com>
>To: JSTONE@i-koda.com
>Message-ID: <1001962724.1.1487964592286@jake-yoga3>
>Subject: K;lj'l;hgjkl 中 中 中 中 中
>MIME-Version: 1.0
>Content-Type: multipart/mixed; boundary="----=_Part_0_59694987.1487964592179"
> 
> ------=_Part_0_59694987.1487964592179 Content-Type: text/plain; charset=Cp1252 Content-Transfer-Encoding: quoted-printable
> 
> Dear Ghjkhgjkl,
> 
> K;lj'l;hgjkl =E4=B8=AD =E4=B8=AD =E4=B8=AD =E4=B8=AD
> =E4=B8=AD=E4=B8=AD =E4=
> =B8=AD =E4=B8=AD =E4=B8=AD =E4=B8=ADhttps://www.eee.com/delivery/dsfr?uf= t=3D1012770&c=3D1012764=E4=B8=AD =E4=B8=AD =E4=B8=AD =E4=B8=AD
> =E4=B8=AD
> ------=_Part_0_59694987.1487964592179--

电子邮件方式:

public boolean send() throws TestReportingException, MessagingException
{
try
{

Properties mailProps = new Properties();
// Set properties required to connect to Gmail's SMTP server
mailProps.put("mail.smtp.host", "smtp.gmail.com");
mailProps.put("mail.smtp.port", "587");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.smtp.starttls.enable", "true");
mailProps.put("mail.mime.charset", "utf-8");

// Create a username-password authenticator to authenticate SMTP
// session
Authenticator authenticator = new Authenticator()
{
    // override the getPasswordAuthentication method
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(username, password);
    }
};

// Create the mail session
Session session = Session.getDefaultInstance(mailProps, authenticator);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setHeader("Content-Type", "text/plain; charset=UTF-8");
// 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
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html; charset=utf-8");
// Now set the actual message
messageBodyPart.setText(message);
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);
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);
}
}

1
所以,你有意将一个由变量mailProps引用的Properties初始化,然后使用另一个变量指定的Properties初始化你的邮件Session吗? - John Bollinger
1
在您呈现的代码执行时,是否有可能默认邮件会话已经使用不同的属性进行初始化?只有在第一次初始化默认实例时才会使用您在Session.getDefaultInstance()中指定的属性。 - John Bollinger
1
请发布您的实际代码。当前的代码中有 mailPropsmaileProps,这可能会导致编译错误或严重的错误。请参阅 http://stackoverflow.com/help/mcve。 - Roland Illig
我解决了属性问题,但它并没有解决问题。 - Jake
你好,你解决了这个问题吗?如果有的话,请提供一些更新。 - Bằng
2个回答

4

messageBodyPart.setText会覆盖你使用messageBodyPart.setContent所做的操作。因此,可以使用以下方法代替两者:

// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText(message, "utf-8", "html");

2

您发布的电子邮件编码错误,因为Cp1252无法编码中文字符。发件人需要将内容类型设置为“text/plain; charset=UTF-8”,并使用RFC 2047编码对主题进行编码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接