简体   繁体   English

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

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

What is the difference between these two functions as they provide the same result in JavaMail API? 这两个函数在JavaMail API中提供相同的结果有什么区别?

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

Suppose if you want to send a plane text then use setText() method. 假设您要发送平面文本,请使用setText()方法。 If you want to send the content of the html code, then you can go for setContent() . 如果要发送html代码的内容,则可以使用setContent()

Keep One point that, setText() and setContent() will override each other. 请记住, setText()setContent()会相互覆盖。 Just use the setText() method that allows you to specify the charset and text type. 只需使用允许您指定字符集和文本类型的setText()方法即可。

For Ex : 对于Ex:

The below line send plain text 下一行发送纯文本

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

and this one will send html content 这将发送html content

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

the text message will display in the header <h1> size. 文本消息将以标题<h1>大小显示。

setText(....) is like setContent(..., "text/plain") where as setContent(..., ...) gives you more control over what MIME type you want to use. setText(....)就像setContent(..., "text/plain") ,其中setContent(..., ...)让您更好地控制要使用的MIME类型。

So, in your example textBody.setContent(bodyText, "text/html"); 因此,在您的示例textBody.setContent(bodyText, "text/html"); will override the previous call textBody.setText(bodyText); 将覆盖先前的调用textBody.setText(bodyText); and change the content's MIME type from text/plain to text/html . 并将内容的MIME类型从text/plain更改为text/html

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

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