在 javax.mail api 中为 mime 多部分/相关消息的内容类型设置“开始”属性
Posted
技术标签:
【中文标题】在 javax.mail api 中为 mime 多部分/相关消息的内容类型设置“开始”属性【英文标题】:set "start" attribute in content-type for mime multipart/related message in javax.mail api 【发布时间】:2012-08-04 06:24:44 【问题描述】:如何在 MIME 多部分/相关消息的内容类型中设置 start
属性?
更准确地说,我想知道如何在以下示例中为根附件设置start
属性,该示例取自https://www.rfc-editor.org/rfc/rfc6362:
--OUTER-BOUNDARY
Content-type: multipart/related; boundary="INNER-BOUNDARY";
start="<root.attachment>"; type="application/xml"
--INNER-BOUNDARY
Content-type: application/xml
Content-ID: <root.attachment>
[XML DOCUMENT]
--INNER-BOUNDARY
Content-type: application/pdf
Content-ID: <2nd.attachment>
[PDF DOCUMENT]
--INNER-BOUNDARY--
--OUTER-BOUNDARY
我无法在 javax.mail
api 中找到它。请帮忙。
【问题讨论】:
【参考方案1】:我最近一直在努力解决这个问题,下面的代码是我能想到的最好的代码(实际上没有其他任何东西对我有用):
public class MultipartGenerator
//Let's assume the static members below
//hold our message parts content
//an the instances of arrays of byte
private static final byte [] ROOT_BYTES = new byte[]/* ... bytes ... */;
private static final byte [] ATTCH_1_BYTES = new byte[]/* ... bytes ... */;
private static final byte [] ATTCH_2_BYTES = new byte[]/* ... bytes ... */;
/**
* Generate multipart with headers
*
* @return javax.mail.Multipart instance
*/
public static Multipart generateMultipart()
//This is our root MimeBodyPart,
//content-id equals 'rootcid'
//content-type equals 'roottype/rootsubtype'
InternetHeaders ih0 = new InternetHeaders();
ih0.addHeader("Content-Type", "roottype/rootsubtype");
ih0.addHeader("Content-Transfer-Encoding", "binary");
ih0.addHeader("Content-ID", "rootcid");
MimeBodyPart rootBodyPart = new MimeBodyPart(ih0, ROOT_BYTES);
//This is a body part wrapping first message attachment
InternetHeaders ih1 = new InternetHeaders();
ih1.addHeader("Content-Type", "text/plain; name=attachment1.txt");
ih1.addHeader("Content-Transfer-Encoding", "binary");
ih1.addHeader("Content-Location", "attachment1.txt");
ih1.addHeader("Content-ID", "a00");
MimeBodyPart attch1BodyPart = new MimeBodyPart(ih1, ATTCH_1_BYTES);
//This is a body part wrapping second message attachment
InternetHeaders ih2 = new InternetHeaders();
ih2.addHeader("Content-Type", "text/plain; name=attachment2.txt");
ih2.addHeader("Content-Transfer-Encoding", "binary");
ih2.addHeader("Content-Location", "attachment2.txt");
ih2.addHeader("Content-ID", "a01");
MimeBodyPart attch2BodyPart = new MimeBodyPart(ih2, ATTCH_2_BYTES);
//This is our desired multipart, this is where things turn a bit dirty
//No success with setting the parameters in a different way
Multipart multipart = new MimeMultipart("related;start=\"<rootcid>\";type=\"roottype/rootsubtype\"");
multipart.addBodyPart(rootBodyPart,0);
multipart.addBodyPart(attch1BodyPart);
multipart.addBodyPart(attch2BodyPart);
return multipart;
可能有更好的方法来处理这项任务,但是我找不到。
【讨论】:
以上是关于在 javax.mail api 中为 mime 多部分/相关消息的内容类型设置“开始”属性的主要内容,如果未能解决你的问题,请参考以下文章
从 MimeBodyPart javax.mail.message 中删除两个连字符
如何有效地使用 javax.mail API 发送批量邮件? & 我们可以使用重用认证会话来提高速度吗?