从 MimeMessage 到字节数组的转换
我需要将 MimeMessage 转换为字节数组,但在转换时某些字符的编码不正确。代码如下所示:
// message is MimeMessage
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
byte[] bytes = baos.toByteArray();
此转换无法正常工作,因为输出我收到格式错误的电子邮件正文:
<html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"en" lang=3D"en"
>
<body style=3D"background-color: #ffffff;" >
...
3D 不应出现在此 (xmlns=3D"http:)。我可以删除它,但这不是一个安全的解决方案,我可能会意外地从电子邮件正文中删除一些内容
任何提示可能会有所帮助。
I need to convert MimeMessage into byte array, but while converting some characters are not coded correctly. Code lookis like this:
// message is MimeMessage
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
byte[] bytes = baos.toByteArray();
This conversion doesn't work correctly, as output I'm receving wrong formatted email body:
<html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"en" lang=3D"en"
>
<body style=3D"background-color: #ffffff;" >
...
3D should not be present in this (xmlns=3D"http:). I could remove it but this is not a safe solution, I might accidentally remove some content from the email body.
Any tip may help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 mime 消息包含 Quoted-Printable Encoding,请参阅 MIME RFC 1521,因此您需要在保存之前对其进行解码。
您应该能够使用 javax.mail.internet.MimeUtility.decode 来实现此目的。
Your mime message contains Quoted-Printable Encoding, see MIME RFC 1521, so you need to decode it before saving it.
You should be able to use javax.mail.internet.MimeUtility.decode for this.