Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Sending Html


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

For better understanding of this example, learn the steps of sending 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

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

Example of sending email with html content using JavaMail API

  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.activation.*;
  5. class SendHtmlEmail
  6. {
  7.    public static void main(String [] args)
  8.    {
  9.       String host=“mail.Kreationnext.com”;//change accordingly
  10.       String to=“sonoojaiswal1987@gmail.com”;//change accordingly
  11.       final String user=“sonoojaiswal@Kreationnext.com”;//change accordingly
  12.       final String password=“xxxxx”;//change accordingly
  13.       Properties properties = System.getProperties();
  14.       properties.setProperty(“mail.smtp.host”, );
  15.       properties.put(“mail.smtp.auth”“true”);
  16.       Session session = Session.getDefaultInstance(properties,
  17.     new javax.mail.Authenticator() {
  18.      protected PasswordAuthentication getPasswordAuthentication() {
  19.       return new PasswordAuthentication(user,password);
  20.      }
  21.       });
  22.       try{
  23.          MimeMessage message = new MimeMessage(session);
  24.          message.setFrom(new InternetAddress(user));
  25.          message.addRecipient(Message.RecipientType.TO,
  26.                                   new InternetAddress(to));
  27.         message.setSubject(“HTML Message”);
  28.         message.setContent(“<h1>sending html mail check</h1>”,“text/html” );
  29.        Transport.send(message);
  30.          System.out.println(“message sent….”);
  31.       }catch (MessagingException ex) {ex.printStackTrace();}
  32.    }
  33. }
download this example to send email with html content
Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac SendHtmlEmail.java
run by c:\> java SendHtmlEmail

Comments

comments