java 为 MimeMessage 设置内容类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13135593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:37:46  来源:igfitidea点击:

Setting Content-Type for MimeMessage?

javajavamailmime-typesmime-message

提问by M Sach

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

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

  1. Mime body part containing plain text, html text(like some letters in bold in body)
  2. Second mime body part containing an attachment,
  3. Third mime body part containing one inline image (which is being referred from body with cid)
  1. 包含纯文本、html 文本的 Mime 正文部分(例如正文中的一些粗体字母)
  2. 包含附件的第二个哑剧身体部分,
  3. 包含一个内嵌图像的第三个 mime 正文部分(从带有 cid 的正文引用)

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

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

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

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

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

multipart/alternative建议用于 html,multipart/mixed建议用于附件,multipart/related建议用于内联。我正在使用它们,那么完整消息和不同正文部分的内容类型应该是什么?

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.

仅供参考,我试图复制上述场景,即我既没有为整体 MimeMessage 也没有为正文部分设置内容类型。

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

但我仍然得到了预期的内容,例如纯文本、正文中的粗体字母、附件、james 上的内嵌图像在正确的位置

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?

为什么 James 在没有设置内容类型的情况下解释了 mime 消息和正文部分,它为什么以正确的方式显示它们?

Code For Reference

代码参考

  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);

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

使用上面的代码,我在与 James 集成的 ThunderBird 中的正确位置获得了正确的 html 文本、纯文本、内嵌图像和附件。

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

所以,我不明白何时,何地设置multipart/mixedmultipart/alternativemultipart/related如Content-Type或没有邮件服务器在内部设置呢?

采纳答案by Bill Shannon

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".

这意味着三个嵌套的多部分。除了默认的“混合”之外,您需要为每个多部分片段指定子类型。

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

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

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.

您可以通过摆脱 multipart/related 并仅让 html 文本引用 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.

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