javax邮件:UTF-8编码问题 [英] javax mail: UTF-8 encoding issue

查看:292
本文介绍了javax邮件:UTF-8编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几个与此有关的问题,但是都没有解决我的问题.

I have seen several questions about this, but none have solved my problem.

我有一封带有pdf附件的中文电子邮件. 在MultiPart电子邮件中包含所有文本之前,所有文本都是有效的UTF-8.

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:

我已经解决了属性问题.我的错误仍然存​​在

I have fixed the properties issue. My bug remains

电子邮件标题

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

推荐答案

messageBodyPart.setText会覆盖您对messageBodyPart.setContent所做的操作.而不是同时执行以下操作:

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

这篇关于javax邮件:UTF-8编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆