Html Formatted Text In An Email In Java
try{ String msg='Happy BirthDay Dear, '+name.toUpperCase()+' !!! Have a Great Day. \n \n Thank You \n Seva Development '; MimeMessage messa
Solution 1:
Try setting helper.setContent(htmlMsg, "text/html");
Solution 2:
You didn't specified content type of mail. In which case it is sent in plain.
Try setting content type
helper.setContent(htmlMsg, "text/html; charset=\"utf-8\"");
Now when you open this mail with any email client, it will read it in html format.
You can also set multiple formats by using MimeMultitype
Multipartmultipart=newMimeMultipart("alternative");
BodyPart messageBodyPart;
// PLAIN TEXT
messageBodyPart = newMimeBodyPart();
messageBodyPart.setContent(textBody, "text/plain; charset=\"utf-8\"");
multipart.addBodyPart(messageBodyPart);
// HTML TEXT
messageBodyPart = newMimeBodyPart();
messageBodyPart.setContent(htmlBody, "text/html; charset=\"utf-8\"");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Solution 3:
Since you are using MimeMessageHelper
.Try below.
MimeMessageHelperhelper=newMimeMessageHelper(message,true);
helper.setText(msg, true);
Use MimeMessageHelper.setText(emailContent,true) method. The boolean true
flag indicates html content.
Solution 4:
This worked for me, I have changed two line code, Thank you all for your contribution
privatevoidsendEmail(String email,String name)throws Exception{
Thread thread=newThread(){
@Overridepublicvoidrun() {
try{
String msg="Dear<b> "+name.toUpperCase()+" </b>,<p> On Behalf of someone we would like to wish you a Many many Happy returns of the day</p> <p style=color:red;>Happy Birthday and Have a Great Day.</p>\n \n Thank You!";
MimeMessagemessage= mailSender.createMimeMessage();
MimeMessageHelperhelper=newMimeMessageHelper(message,true);
helper.setTo(email);
helper.setText(msg);
message.setContent(msg, "text/html");
helper.setSubject("BirthDay");
mailSender.send(message);
}catch (Exception e){}
}
};
thread.start();
}
Post a Comment for "Html Formatted Text In An Email In Java"