4

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

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);
}
}
Jake
  • 4,322
  • 6
  • 39
  • 83
  • 1
    So, is it intentional that you initialize a `Properties` referenced by variable `mailProps`, and then initialize your mail `Session` with a `Properties` designated by a different variable? – John Bollinger Feb 24 '17 at 20:29
  • 1
    Is it possible that by the time the code you present is executed, the default mail session has been initialized with different properties? The ones you specify in `Session.getDefaultInstance()` are used only when the default instance is first initialized. – John Bollinger Feb 24 '17 at 20:32
  • 1
    Please post your actual code. The current code has both `mailProps` and `maileProps`, which either produces a compile error or is a serious bug. See http://stackoverflow.com/help/mcve. – Roland Illig Feb 24 '17 at 20:43
  • I fixed the properties issue but it doesn't fix the problem – Jake Feb 24 '17 at 21:01
  • Hello, did you solve the problem? And if so, please give some updates. – Bằng Dec 03 '20 at 11:19

2 Answers2

4

messageBodyPart.setText overwrites what you did with messageBodyPart.setContent. Instead of both, do this:

// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText(message, "utf-8", "html");
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
2

The email you posted is encoded wrongly, since Cp1252 cannot encode Chinese characters. The mail sender needs to set the content-type to "text/plain; charset=UTF-8" and encode the subject using RFC 2047 encoding.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121