JavaMail API - SPLessons

JavaMail Send HTML

Chapter 10

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Send HTML

JavaMail Send HTML

shape Description

Some times user may need to send some text in any format then programatically developer needs to mention the content type, this is same as previous chapters but methods are little different to use. Sending HTML through JavaMail API is similar to sending email through it. But here using setContent() method to represent the HTML content as a second argument like “test/html” in the message. Here by using JangoSMTP server sending the emails and HTML content.

shape Conceptual
figure

Follow the below flow steps for JavaMail Send HTML.
The process of sending HTML involves session object, MimeMessage object, and setContent() method, transport object. Here the To address, subject of message is set in MimeMessage object to access.

shape Examples

Let us see how to send HTML email with example shown below.

package firstmail;
import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  
public class sendhtml  
{  
   public static void main(String [] args)  
   {     
      String host="9saireddy@gmail.com";//change accordingly  
      String to="9saireddy@gmail.com";//change accordingly  
      final String user="9saireddy@gmail.com";//change accordingly  
      final String password="*******";//change accordingly     
      Properties props = new Properties();  
      props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
      "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");   
      Session session = Session.getDefaultInstance(props,  
    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>This message has been sent from splessons.com</h1>","text/html" );       
       Transport.send(message);  
         System.out.println("Message has been Sent..");  
      }catch (MessagingException ex) {ex.printStackTrace();}  
   }  
} 

Output
When compile the program following is the message will be displayed on the console.

Message has been Sent..

Now check the mail.

Summary

shape Key Points

  • JavaMail Send HTML – Sending HTML through JavaMail API is similar to sending email through it.
  • JavaMail Send HTML – Here using setContent() method to represent the HTML content as a second argument like “test/html” in the message.
  • JavaMail Send HTML – Here by using JangoSMTP server sending the emails and HTML content.
  • JavaMail Send HTML – Use MineMessage Object for sending HTML email.