【问题标题】:Apache Commons Email encode attach with base64Apache Commons 电子邮件编码附加 base64
【发布时间】:2012-04-24 11:46:23
【问题描述】:

我正在尝试通过apache.commons.mail 发送一个base64 编码的文件,但我无法将Content-Transfer-Encoding: base64 标头放到应该去的地方。

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

email.addTo("to@example.com");
email.setFrom("from@example.com");
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 编码的?

【问题讨论】:

    标签: java base64 apache-commons-email


    【解决方案1】:

    最简单的解决方案是这样做:

    // 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。

    希望对你有帮助。

    【讨论】:

    • 我用答案来操纵身体传输编码,效果也很好。我只需要放弃removeHeader 电话。
    【解决方案2】:

    您可以尝试覆盖 attach 方法并在其中设置 Content-Transfer-Encoding 标头。默认情况下,框架不会为您设置它或干净地公开 MIME bodyPart。

    【讨论】:

    • 这可能会...我已经成功地将MimeMultipart() (javax.mail) 添加为commons.mail 的外部部分。但还是有些不对劲。我可能弄乱了 Base64 编码。让我进一步挖掘......
    • 接受您的回答,因为它让我走上了正轨。发表我的作为参考。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-09-17
    • 2017-08-14
    • 2018-09-22
    • 2011-12-30
    • 1970-01-01
    • 2012-02-06
    相关资源
    最近更新 更多