I have seen several questions about this, but none have solved my problem.
I have a Chinese email with a pdf attachment. All the text is valid UTF-8 until it is included in the MultiPart email.
Problem: The text in the email is garbage characters when it gets to the recipient. The email header shows it is not encoded correctly.
I include both my code and the email header below:
EDIT: I have fixed the properties issue. My bug remains
Email Header
> eturn-Path: <[email protected]>
>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 <[email protected]>
> (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 <[email protected]>
>To: [email protected]
>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--
Email Method: Email Method:
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);
}
}
Properties
referenced by variablemailProps
, and then initialize your mailSession
with aProperties
designated by a different variable? – John BollingerSession.getDefaultInstance()
are used only when the default instance is first initialized. – John BollingermailProps
andmaileProps
, which either produces a compile error or is a serious bug. See stackoverflow.com/help/mcve. – Roland Illig