I'm using Java Mail API for send email. I have succeed sending email with plain text but I don't know how to add Html inside body of the email. When I try to add HTML code, it becomes plain text. This is my code for sending email.
public static void send(String to, String subject, String myHTML code)
{
final String from_user = myemail";
final String pass = "mypass";
final String SMTP_HOST = "smtp.gmail.com";
final String from_name = "Nhan";
//1st step) Get the session object
Properties props = System.getProperties();
props.put("mail.smtp.host", SMTP_HOST);//change accordingly
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from_user,pass);
}
});
//Session session = Session.getDefaultInstance(props);
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from_user, from_name));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(myHTMLCode);
//3rd step)send message
Transport.send(message);
System.out.println("Done");
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
So please tell me how to add HTML code inside the body of email. Thanks so much for your help, sorry I'm newbie.