Sending email with Html content

As we send the email, we can send the html content moreover.

For better comprehension of this illustration, take in the progressions of sending email utilizing JavaMail API first.

For getting or sending the email utilizing JavaMail API, you have to stack the two jug records:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.


Sample of sending email with html substance utilizing 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.javatportal.com";//change as needs be   
      String to="mithileshgniit@gmail.com";//change as needs be   
      final String user="mithilesh@javatportal.com";//change as needs be   
      final String password="*******";//change as needs be   
  
      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 file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac SendHtmlEmail.java
run by c:\> java SendHtmlEmail