如何添加一个MimeMultipart到另一个? [英] How to add a MimeMultipart to another one?

查看:296
本文介绍了如何添加一个MimeMultipart到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常愚蠢的问题,但我正在尝试撰写一封电子邮件,例如建议此处

This is possibly a very stupid question, but I'm trying to compose an Email message like suggested here


  • multipart / mixed


    • multipart / alternative


      • text / html

      • text / plain

      所以我有

      MimeMultipart altPart = new MimeMultipart("alternative");
      
      BodyPart textPart = new MimeBodyPart();
      textPart.setContent("someText", "text/plain");
      altPart.addBodyPart(textPart);
      
      BodyPart htmlPart = new MimeBodyPart();
      htmlPart.setContent("someHtml", "text/html");
      altPart.addBodyPart(htmlPart);
      
      MimeMultipart mixedPart = new MimeMultipart("multipart/mixed");
      

      ,需要将 altPart 添加到code> mixedPart ,但我不能唯一的添加方法只接受 BodyPart 。 WTF?

      and need to add altPart to mixedPart, but I can't as the only adding method accepts BodyPart only. WTF?

      请注意,与此处不同

      推荐答案

      你需要包装你的 MimeMultipart 另一个 MimeBodyPart ,使用 MimeBodyPart.setContent(Multipart mp)方法。然后,您可以将 MimeBodyPart 添加到 mixedPart Object:

      You need to wrap your MimeMultipart in another MimeBodyPart, using the MimeBodyPart.setContent(Multipart mp) method. Then you can add the MimeBodyPart to the mixedPart Object:

      MimeMultipart alternativeMultipart = new MimeMultipart("alternative");
      
      BodyPart textPart = new MimeBodyPart();
      textPart.setContent("someText", "text/plain");
      alternativeMultipart.addBodyPart(textPart);
      
      BodyPart htmlPart = new MimeBodyPart();
      htmlPart.setContent("someHtml", "text/html");
      alternativeMultipart.addBodyPart(htmlPart);
      
      MimeBodyPart alternativeBodyPart = new MimeBodyPart();
      alternativeBodyPart.setContent(alternativeMultipart);
      
      MimeMultipart mixedMultipart = new MimeMultipart("mixed");
      mixedMultipart.addBodyPart(alternativeBodyPart);
      
      MimeBodyPart textPart1 = new MimeBodyPart();
      textPart1.setContent("someOtherText", "text/plain");
      mixedMultipart.addBodyPart(textPart1);
      

      这篇关于如何添加一个MimeMultipart到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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