Monday 11 March 2013

Mail HTML content in body in java

Leave a Comment
package com;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

class SendHtmlEmail
{
   public static void main(String [] args)
   {

      String to="abc@hotmail.com";//change accordingly
      final String user="abc@gmail.com";//change accordingly
      final String password="****";//change accordingly

      Properties properties = System.getProperties();
      properties.put("mail.smtp.host", "smtp.gmail.com");
      properties.put("mail.smtp.socketFactory.port", "465");
      properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.port", "465");
      
      
      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");
        String htmlMessage ="

sending html mail check

"; htmlMessage = htmlMessage + "

Helooooooooooooooooo

"; message.setContent(htmlMessage,"text/html" ); Transport.send(message); System.out.println("message sent...."); }catch (MessagingException ex) {ex.printStackTrace();} } }

0 comments:

Post a Comment