javax 邮件:UTF-8 编码问题

2024-01-02

我已经看到了几个与此相关的问题,但没有一个能解决我的问题。

我有一封带有 pdf 附件的中文电子邮件。 所有文本在包含在多部分电子邮件中之前都是有效的 UTF-8。

Problem:电子邮件中的文本到达收件人时是垃圾字符。电子邮件标头显示其编码不正确。

我在下面包含了我的代码和电子邮件标题:

EDIT:我已经解决了属性问题。我的bug依然存在

电子邮件标头

> eturn-Path: <[email protected] /cdn-cgi/l/email-protection>
>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] /cdn-cgi/l/email-protection>
>        (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] /cdn-cgi/l/email-protection>
>To: [email protected] /cdn-cgi/l/email-protection
>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 所做的操作。而不是两者都执行,请执行以下操作:

// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText(message, "utf-8", "html");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

javax 邮件:UTF-8 编码问题 的相关文章

随机推荐

  • Java - 子类调用超级构造函数,该构造函数调用子类方法而不是它自己的方法

    我将从一个代码示例开始 class A public A f When accessed through super call this does not call A f as I had expected public void f I
  • Java 中的 Unicode 符号(箭头)

    我想在我的应用程序中使用以下符号作为按钮 箭头http img402 imageshack us img402 3176 arrowso jpg http img402 imageshack us img402 3176 arrowso j
  • 如何使用 Delphi 读取 Windows 事件日志的内容

    是否有一个类或函数允许您读取 Windows 事件日志 这是你打开后看到的日志事件vwr msc 理想情况下选择一个特定的日志 在我的例子中应用领域登录Windows日志 并根据日期和来源设置过滤器 您可以使用Win32 NTLogEven
  • 来自文字的静态 std::string 对象的宏

    假设我需要调用一个函数foo这需要一个常量std string我的代码中很多地方都引用了 int foo const std string foo bar foo baz 使用像这样的字符串文字调用函数将创建临时的std string对象
  • 在 adb logcat 输出中查看 Android 上 Qt 应用程序的日志记录的最简单方法是什么?

    NB I am notQtCreator 用户 我使用 qmake make 和 androiddeployqt 在构建脚本中构建 Android 应用程序 并使用 adb install 将它们部署到设备 我希望能够在 abd logca
  • git pre-commit hook,将文件添加到索引中

    我正在尝试编写一个简单的预提交挂钩来检查文件是否被修改 如果是 则压缩它并将其添加到当前索引中 如下所示 bin sh was the file modified mf git status grep jquery detectBrowse
  • 同步 AJAX 调用在 Chrome 中冻结之前的代码

    我想在执行同步 AJAX 调用时将按钮更改为加载状态 除了将按钮更改为加载状态的 jQuery 代码 在 Chrome 中 之外 它会冻结 直到 AJAX 调用完成 因此 加载状态将在 de ajax 调用后显示大约 1 毫秒 我在 JSF
  • OpenGL:快速离屏渲染

    我需要使用 OpenGL 在屏幕外渲染大量 数万 图像 我在Windows下运行并使用QT作为框架 解决方案只能是Windows 这并不重要 根据我使用谷歌的发现 有很多选择可以做到这一点本文 http www mesa3d org bri
  • 如何在python中将输入值与mysql数据库值进行比较

    所以我想将输入值与我的数据库值进行比较 如果输入值与数据库的值相同 我想print inputvalue 但如果不一样 我想print Data Does Not Exist 所以我尝试过这段代码 cur connection cursor
  • 是什么让 DCG 谓词变得昂贵?

    我正在构建一个定语从句语法来解析 20 000 段半自然文本 随着我的谓词数据库大小的增长 现在达到 1 200 条规则 解析字符串可能需要相当长的时间 特别是对于 DCG 目前无法解释的字符串 因为我尚未编码语法 对于包含 30 个单词的
  • 将 scotty 帖子的 do 替换为 >>=

    post introduceAnIdea do command lt jsonData json handle command 如何删除 do 并用 gt gt 更改它 post introduceAnIdea jsonData gt gt
  • 为什么网站的 MVC 需要单点入口?

    我看到许多网站的 MVC 实现都有一个单入口点 例如 index php 文件 然后解析 URL 以确定要运行哪个控制器 这对我来说似乎很奇怪 因为它涉及到必须使用 Apache 重写来重写 URL 并且页面足够多 单个文件会变得臃肿 为什
  • 什么是文件描述符,用简单的术语解释一下?

    与维基百科相比 文件描述符的更简化描述是什么 为什么需要它们 比如说 以shell进程为例 它是如何应用的呢 进程表是否包含多个文件描述符 如果是 为什么 简而言之 当您打开文件时 操作系统会创建一个条目来表示该文件并存储有关该打开文件的信
  • Circleci:pip install dlib 失败

    我有一个 python 项目需要dlib 我正在尝试设置 CircleCI 并编写我的config yml如下 Python CircleCI 2 0 configuration file Check https circleci com
  • NestJS:如何在 canActivate 中模拟 ExecutionContext

    我在模拟 Guard 中间件中的 ExecutionContext 时遇到问题 这是我的 RoleGuard 扩展 JwtGuard Injectable export class RoleGuard extends JwtAuthGuar
  • @Transactional注解

    之间有什么区别 为整个类添加 Transactional 注释 为每个方法添加 Transactional 注释 使用 spring 和 Hibernate 基本上 如果你用 Transactional http static spring
  • 如何“扫描”当前安装的 VCL 组件的完整列表

    我还没有找到真正满意的答案这个问题 https stackoverflow com questions 691989 full vcl class browser for delphi 现在正在考虑推出自己的 我有 ModelMaker 和
  • Entity Framework 4 v2 中与 POCO 的一对一关系

    我一直在寻找有关如何在 EF4v2 中与 POCO 建立一对一关系的示例 我发现很多例子只展示了如何创建一对多或多对多 你有这方面的资源吗 这对我有用 using System using System Collections Generi
  • 国际象棋:高分支因子

    我正在尝试开发一个简单的国际象棋引擎 但我在其性能方面遇到了困难 我已经通过 alpha beta 修剪和迭代加深 没有任何额外的启发式 实现了 Negamax 但是我无法获得超过 3 4 层的合理搜索时间 以下是我的程序从游戏开始时的日志
  • javax 邮件:UTF-8 编码问题

    我已经看到了几个与此相关的问题 但没有一个能解决我的问题 我有一封带有 pdf 附件的中文电子邮件 所有文本在包含在多部分电子邮件中之前都是有效的 UTF 8 Problem 电子邮件中的文本到达收件人时是垃圾字符 电子邮件标头显示其编码不