Apache Commons 电子邮件使用 base64 进行编码附加

2023-12-04

我正在尝试通过以下方式发送 base64 编码的文件apache.commons.mail我似乎无法理解Content-Transfer-Encoding: base64标头应该去的地方。

// Create the email
MultiPartEmail email = new MultiPartEmail();
email.setSmtpPort(587);
email.setDebug(false);
email.setHostName("smtp.gmail.com");
email.setAuthentication("[email protected]", "password");
email.setTLS(true);

email.addTo("[email protected]");
email.setFrom("[email protected]");
email.setSubject("subject");

email.attach(new ByteArrayDataSource(
     Base64.encodeBase64(attachFull.getBytes()), "text/plain"), 
     "samplefile.txt", 
     "sample file desc", 
     EmailAttachment.ATTACHMENT
);

这就是收件人收到的信息。

------=_Part_0_614021571.1334210788719
Content-Type: text/plain; charset=Cp1252; name=texto.txt
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=samplefile.txt
Content-Description: sample file desc

如何指定文件是 Base64 编码的?


最简单的解决方案是执行以下操作:

// create a multipart leg for a specific attach
MimeMultipart part = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler (new DataHandler(new ByteArrayDataSource(attachFull.getBytes(), "text/plain")));
messageBodyPart.removeHeader("Content-Transfer-Encoding");
messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");
part.addBodyPart(messageBodyPart);
email.addPart(part);

javax 会自动将您的文件转换为 base64。

希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apache Commons 电子邮件使用 base64 进行编码附加 的相关文章

随机推荐