BodyMimePart类

时间:2017-11-18 14:40:28

标签: java android javamail mime-types gmail-api

这两个函数有什么区别,因为它们在JavaMail API中提供相同的结果?

Multipart multipart = new MimeMultipart();
BodyPart textBody = new MimeBodyPart();
textBody.setText(bodyText);
textBody.setContent(bodyText, "text/html") ;
multipart.addBodyPart(textBody);

2 个答案:

答案 0 :(得分:3)

假设您要发送平面文本,请使用setText()方法。如果您要发送html代码的内容,则可以转到setContent()

保持一点,setText()setContent()将相互覆盖。只需使用允许您指定字符集和文本类型的setText()方法。

对于Ex:

以下行发送纯文本

plainTextPart.setText("This is plain text message", "UTF-8");

这个人会发送html content

htmlTextPart.setContent("<h1>This is plain HTML message</h1>", "text/html;charset=UTF-8");

短信将显示在标题<h1>大小。

答案 1 :(得分:2)

art: { height: "70%", aspectRatio: 1, marginRight:10, marginTop:10, marginBottom:10, }, setText(....)类似,setContent(..., "text/plain")可让您更好地控制要使用的MIME类型。

因此,在您的示例中,setContent(..., ...)将覆盖之前的调用textBody.setContent(bodyText, "text/html");,并将内容的MIME类型从textBody.setText(bodyText);更改为text/plain