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

Viewed 8071

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);
2 Answers

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.

Related