24Jan

Sending email in JAVA with HTML content

To Send email using JAVAMail API, you need to download the following two jar files on your package.

  • mail.jar
  • activation.jar

go to the Oracle site to download the latest version.

Example of sending email with html content using JavaMail API

import java.util.*;  

import javax.mail.*;  

import javax.mail.internet.*;  

import javax.activation.*;  

  

class SendHtmlEmail  

{  

   public static void main(String [] args)  

   {  

      String host=”smpt.gmail.com”;//change accordingly  

      String to=”senderemail@gmail.com”;//change accordingly  

      final String user=”youremail@gmail.com”;//change accordingly  

      final String password=”yourgmailpassword”;//change accordingly  

        Properties properties = System.getProperties();  

      properties.setProperty(“mail.smtp.host”, );  

      properties.put(“mail.smtp.auth”, “true”);  

     Session session = Session.getDefaultInstance(properties,  

    new javax.mail.Authenticator() {  

     protected PasswordAuthentication getPasswordAuthentication() {  

      return new PasswordAuthentication(user,password);  

     }  

      });  

        try{  

         MimeMessage message = new MimeMessage(session);  

         message.setFrom(new InternetAddress(user));  

         message.addRecipient(Message.RecipientType.TO,  

                                  new InternetAddress(to));  

          message.setSubject(“HTML Message”);  

        message.setContent(“<h1>sending html mail check</h1>”,”text/html” );  

           Transport.send(message);  

         System.out.println(“message sent….”);  

      }catch (MessagingException ex) {ex.printStackTrace();}  

   }  

}  

Leave a Reply

Your email address will not be published. Required fields are marked *