繁体   English   中英

BodyMimePart类中的setText()和setContent()有什么区别

[英]What is difference between setText() and setContent() in BodyMimePart class

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

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

假设您要发送平面文本,请使用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>大小显示。

setText(....)就像setContent(..., "text/plain") ,其中setContent(..., ...)让您更好地控制要使用的MIME类型。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM