为MimeMessage设置Content-Type? [英] Setting Content-Type for MimeMessage?

查看:883
本文介绍了为MimeMessage设置Content-Type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mime消息的内容类型感到困惑。说我有一个mime消息。它是一个多部分的消息,正文部分是这样的

I have one confusion about content type of mime message. Say I have a mime message. It is a multipart message and the body parts are like this


  1. Mime正文部分包含纯文本,html文本(就像$ b中的一些字母一样) $ b bold in body)

  2. 包含附件的第二个mime身体部位,

  3. 包含一个内联图像的第三个mime身体部位(正在引用带cid的正文)

当我创建正文部分时,我应该明确设置顶级mime消息的内容类型,然后是每个正文部分?

When I am creating the body part, should I explicitly set the content type for top mime message and then each body part?

如果是,那么它们在上面的例子中应该是什么?

If yes, what should they be in the above example?

建议用于html, multipart / mixed ,建议内联使用 multipart / related 。我正在使用所有这些,那么什么应该是内容类型的完整消息和不同的身体部位?

multipart/alternative is suggested for html, multipart/mixed is suggested for attachments, multipart/related is suggested for inline. I am using all of them, so what should be content-Type for complete message and different body parts?

仅仅是为了获取信息我尝试复制上面的情况,我没有为整体MimeMessage和身体部位设置内容类型。

Just for information I tried to replicate above scenario where I did not set the content type neither for the overall MimeMessage nor for body parts.

但我仍然得到预期的内容,如纯文本,正文中的粗体字母,附件,内嵌图像等詹姆斯在正确的位置

But still I get the expected stuff like plain text, Bold letters in body, attachment, inline image on james at right place

为什么詹姆斯在没有设置内容类型的情况下解释哑剧信息和身体部位,为什么以正确的方式展示它们?

How come James is interpreting the mime message and body parts without setting the content type, and how come it is displaying them in right fashion?

参考代码

  MimeMessage   msg = new MimeMessage(mailSession);
  MimeMultipart mpart = new MimeMultipart();
  MimeBodyPart bp = new MimeBodyPart();
  bp.setText("plain text and html text like<b>Test</>", CHARSET_UTF_8, MESSAGE_HTML_CONTENT_TYPE);
  // add message body
  mpart.addBodyPart(bp);

 // adding attachment
  MimeBodyPart bodyPart = new MimeBodyPart();
  bodyPart.setFileName("WordFile1");
  file = new File("word file");
  DataSource source = new FileDataSource(file);
  bodyPart.setDataHandler(new DataHandler(source));
  mpart.addBodyPart(bodyPart);


 // adding image inline
  MimeBodyPart bodyPart2 = new MimeBodyPart();
  bodyPart2.setFileName("inline image");
  file2 = new File("image1");
  DataSource source2 = new FileDataSource(file);
  bodyPart2.setDataHandler(new DataHandler(source));
  bodyPart2.setDisposition(MimeBodyPart.INLINE);
  bodyPart2.setHeader("Content-ID", "Unique-CntentId");
  bodyPart2.setHeader("Content-Type", "image/jpeg");
  mpart.addBodyPart(bodyPart2);

  // At last setting multipart In MimeMessage
  msg.setContent(mpart);

使用上面的代码,我得到了正确的html文本,纯文本,内嵌图像和右边的附件在ThunderBird中与James合并。

With the above code, I get the correct html text, plain text, inline image and attachments at right place in ThunderBird integrated with James.

所以我不明白何时何地设置 multipart / mixed multipart / alternative multipart / related as Content-Type还是邮件服务器在内部设置它?

So I don't understand when and where to set multipart/mixed, multipart/alternative, multipart/related as Content-Type or does the mail server internally set it?

推荐答案

如果我理解你要做什么,你想要一个具有这种结构的消息:

If I understand what you're trying to do, you want a message with this structure:

  multipart/mixed
    multipart/alternative
      text/plain - a plain text version of the main message body
      multipart/related
        text/html - the html version of the main message body
        image/jpeg - an image referenced by the main body
    application/octet-stream (or whatever) - the attachment

这意味着三个嵌套的多部分。您需要为默认的混合以外的每个多部分指定子类型。

That means three nested multipart pieces. You'll need to specify the subtype for each multipart piece other than the default "mixed".

multipart / mixed和multipart / alternative部分相对简单。多部分/相关部分更复杂,您可能需要阅读 RFC 2387 和/或找一些其他教程可以帮助你。

The multipart/mixed and multipart/alternative pieces are relatively straightforward. The multipart/related piece is more complicated and you might want to read RFC 2387 and/or find some other tutorials to help you with that.

你可以通过删除multipart / related来简化结构,只需将html文本引用到互联网上的某个图像。

You can simplify the structure by getting rid of the multipart/related and just having the html text reference an image somewhere on the internet.

您还应该测试所有您关心的邮件阅读器是否正确显示具有此结构的邮件。有些邮件阅读器比其他具有复杂结构的邮件阅读器做得更好。

You should also test that a message with this structure is going to be displayed properly by all the mail readers you care about. Some mail readers will do a better job than others with a complicated structure such as this.

这篇关于为MimeMessage设置Content-Type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆