Monday, 23 December 2013

Java - Sending Email using JavaMail API

The Java Mail API provides support for sending and receiving electronic mail messages. The API provides a plug-in architecture where vendor’s implementation for their own proprietary protocols can be dynamically discovered and used at the run time. Sun provides a reference implementation and its supports the following protocols namely,

§  Internet Mail Access Protocol (IMAP)
§  Simple Mail Transfer Protocol (SMTP)
§  Post Office Protocol 3(POP 3)
In this tip, let us see how to send mail using the Java Mail API. Following is the complete code listing for sending a mail from a Java Application,

For this you have to download java-mail-1.4.4.jar


package com.mohan.javaexamples;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendGMail {

            public static void sendMail(String from, String to, String sub, String text)
                                    throws UnsupportedEncodingException, MessagingException {
//using TLS
                        Properties propsTLS = new Properties();
                        propsTLS.put("mail.transport.protocol", "smtp");
                        propsTLS.put("mail.smtp.host", "smtp.gmail.com");
                        propsTLS.put("mail.smtp.auth", "true");
                        propsTLS.put("mail.smtp.starttls.enable", "true");

                        Session session = Session.getDefaultInstance(propsTLS,
                                                new javax.mail.Authenticator() {
                                                            protected PasswordAuthentication getPasswordAuthentication() {
                                                                        return new PasswordAuthentication(
                                                                                                "gmailUsername@gmail.com", "gmailPassword");// Specify the Username and the PassWord
                                                            }
                                                });
                        session.setDebug(true);

                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress("gmailUsername@gmail.com",
                                                "Username"));
                        message.setRecipients(Message.RecipientType.TO,
                                                InternetAddress.parse("gmailReciverUsername@gmail.com"));
message.setSubject("Test mail program  using TLS techniques ....!"); //specify subject
                        message.setText("good job mohan ! keep it up .."); //specify text/body
                         message.setSentDate(new Date());

                        Transport transport = session.getTransport();
                        transport.connect("smtp.gmail.com", 587, "gmailUsername@gmail.com",
                                                "gmailPassword");
                        Transport.send(message, message.getAllRecipients());
                        transport.close();

                        System.out.println("------------------------------------------------------------------------");

//using SSL

                        Properties propSSL = new Properties();
                        propsTLS.put("mail.transport.protocol", "smtps");
                        propSSL.put("mail.smtps.host", "smtp.infostretch.com");
                        propSSL.put("mail.smtp.socketFactory.class",
                                                "javax.net.ssl.SSLSocketFactory");
       propSSL.put("mail.smtp.socketFactory.fallback", "false");
                        propSSL.put("mail.smtps.auth", "true");

                        Session session2 = Session.getDefaultInstance(propSSL,
                                                new javax.mail.Authenticator() {
                                                            protected PasswordAuthentication getPasswordAuthentication() {
                                                                        return new PasswordAuthentication(
                                                                                                "gmailUsername@gmail.com", "gmailPassword");// Specify the Username and the PassWord
                                                            }
                                                });
                        session2.setDebug(true);

                        Message message2 = new MimeMessage(session2);

                        message2.setFrom(new InternetAddress("gmailUsername@gmail.com",
                                                "Username"));
                        message2.setRecipients(Message.RecipientType.TO,
                                                InternetAddress.parse("gmailReciverUsername@gmail.com"));
                        message2.setSubject("Test mail program  using SSL!"); //specify subject
                        message2.setText("good job mohan ! keep it up .."); //specify text/body
                        message2.setSentDate(new Date());

                        Transport transportSSl = session2.getTransport();
                        transportSSl.connect("smtp.gmail.com", 465,"gmailUsername@gmail.com", "gmailPassword");
                        Transport.send(message2, message2.getAllRecipients());
                        transportSSl.close();
            }

            public static void main(String[] args) throws UnsupportedEncodingException,
                                    MessagingException {
                        System.out.println("sending mail .........!");
                        sendMail(null, null, null, null);
            }

}

No comments:

Post a Comment

Getting started with Elasticsearch and Node.js

  In this article we're going to look at using Node to connect to an Elasticsearch deployment, index some documents and perform a simple...