简体   繁体   中英

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?

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. If you want to send the content of the html code, then you can go for setContent() .

Keep One point that, setText() and setContent() will override each other. Just use the setText() method that allows you to specify the charset and text type.

For Ex :

The below line send plain text

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

and this one will send 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.

setText(....) is like setContent(..., "text/plain") where as setContent(..., ...) gives you more control over what MIME type you want to use.

So, in your example textBody.setContent(bodyText, "text/html"); will override the previous call textBody.setText(bodyText); and change the content's MIME type from text/plain to text/html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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