-1

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.

nptn
  • 77
  • 1
  • 3
  • 10
  • 1
    I'm so sorry for not seeing that topic before sending this. Thanks for your help so much – nptn Feb 04 '16 at 03:24

1 Answers1

0

Just change your setText to:

message.setText(myHTMLCode, "utf-8", "html");

fcnorman
  • 1,154
  • 9
  • 19