Sending email with Html content

Sending email with Html content

in this guide, we will discuss about Sending emails with Html content. As we send the email, we can send the HTML content also.

For a better understanding of this example, learn the steps of sending an email using JavaMail API first.

For receiving or sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

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="mail.adglob.in";//change accordingly  
      String to="subratsahoo904@gmail.com";//change accordingly  
      final String user="subratsahoo@adglob.in";//change accordingly  
      final String password="xxxxx";//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();}  
   }  
}  
Load the jar filec:\> set classpath=mail.jar;activation.jar;.;
compile the source filec:\> javac SendHtmlEmail.java
run byc:\> java SendHtmlEmail

Next Topic : Click Here

Leave a Reply