我看到了很多关于这个问题的提问,但是没有一个能解决我的问题。
我有一封带有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);
}
}
mailProps
引用的Properties
初始化,然后使用另一个变量指定的Properties
初始化你的邮件Session
吗? - John BollingerSession.getDefaultInstance()
中指定的属性。 - John BollingermailProps
和maileProps
,这可能会导致编译错误或严重的错误。请参阅 http://stackoverflow.com/help/mcve。 - Roland Illig