Sunday, February 17, 2013

Sending Mail using Java

JavaMail is an API with which we can send and receive E-mail. JavaMail is an platform-independent framework.This whole work  can we done by using SMTP server of gmail over SSL.
This post it focus on only sending emails via gmail.
  • SMTP: Full form is Simple Mail Transfer Protocol, it provide mechanism to send emai, is the protocol in widespread use today. It uses TCP port 25.SMTP connections secured by SSL are known by the shorthand SMTPS, though SMTPS is not a protocol in its own right.While electronic  mail server and other  mail transfer agents use SMTP to send and receive mail messages, user-level client mail applications typically use SMTP only for sending messages to a mail server for  relaying.
  • Secure Sockets Layer (SSL), are cryptographic protocols that provide communication  security over the Internet.SSL encrypt the segments of  network connections at the  Application Layer for the  Transport Layer, using asymmetric cryptography  for key exchange, symmetric encryption for confidentiality, and message authentication codes for message integrity.
JAVA-MAIL API

For sending mail we need two jar files.

The basic java program contain following sub-sections.
  1. Get the session object, that contain all information about host like username,password,etc.
  2. compose the message.
  • Create MimeBodyPart Object and set your message text.
  • Create new MimeBodyPart Object and set DataHandler object to this object.
  • Create multipart object and add  MimeBodayPart object to this.
  • Set multipart object to message object.
  • send message
  •  MIME: Multiple internet mail extension tells the browser what is to be sent, example message format, attachments etc. it is not known as mail transfer protocol but used by mail program.



Explanation of sub-parts are as follows:
  1. Get the session object: The javax.mail.Session class provides two methods to get the object of session, Session.getDefaultInstance() method and Session.getInstance() method. You can use anyone method to get the session object. 
Syntax for Session.getDefaultInstance():There are two methods to get the session object by using the getDefaultInstance() method. It returns the default session.
  • Public static Session GetDeafultInstance(Properties pros)
  •  Public static Session GetDeafultInstance(Properties pros,Authenticator auth)
  2. Compose the Message:javax.mail provide methods to compose the message. But it is abstract class so it's subclass javax.mail.internet.MimeMessage class is mostly used.
to create message you need to pass session to MimeMessage class constructor.
                    MimeMessage message=new MimeMessage(session);
  MimeMessage class further provide many methods, attachments are also handled in this subsection.
  3. Sending: The javax.mail.Transport class provides method to send the message.


To run Basic java programfor sending Email (without attachments), you need :
  • Netbeans (any other IDE)
  •  mail.jar
  • activation.jar
  • username and password of gmail account.
First make new project and add mail.jar and activation.jar in library.
     

Code:


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

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

 String to="xxxx@gmail.com";//change accordingly

  //Get the session object
  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("yourgmailid@gmail.com","password");//change accordingly
   }
  });
 
  //compose message
  try {
   MimeMessage message = new MimeMessage(session);
   message.setFrom(new InternetAddress("yourgmailid@gmail.com"));//change accordingly
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
   message.setSubject("Hello");
   message.setText("Testing.......");
   
   //send message
   Transport.send(message);

   System.out.println("message sent successfully");
 
  } catch (MessagingException e) {throw new RuntimeException(e);}
 
 }
}
     


I'am presently Working on designing GUI for this application, in next post complete code will be provided with GUI design.
Gmail client
 













No comments:

Post a Comment