Sending Email through Gmail Server Using JavaServlets


import java.io.*;

import java.net.*;



import java.util.Properties;

import javax.mail.AuthenticationFailedException;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.servlet.*;

import javax.servlet.http.*;



public class SendEmail extends HttpServlet {



 protected void processRequest(HttpServletRequest request,

                                  HttpServletResponse response)

                   throws IOException, ServletException {



        final String err = "/error.jsp";

        //final String succ = "/success.jsp";



        String from = "abc@gmail.com"    // Sender Email-Id

        String to = "xyz@gmail.com"     // Receiver Email-Id



System.out.println("mail : "+to);

        String subject = "Subject";          //Your Subject.

        String message = "Message"    Your Message



        String login = "abc@gmail.com";        //Sender Mail_Id

        String password = "xyx"; //Sender Mail Password



        try {

            Properties props = new Properties();

            props.setProperty("mail.host", "smtp.gmail.com");

            props.setProperty("mail.smtp.port", "587");

            props.setProperty("mail.smtp.auth", "true");

            props.setProperty("mail.smtp.starttls.enable", "true");



            Authenticator auth = new SMTPAuthenticator(login, password);



            Session session = Session.getInstance(props,auth);



            MimeMessage msg = new MimeMessage(session);

            msg.setText(message);

            msg.setSubject(subject);

            msg.setFrom(new InternetAddress(from));

            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            Transport.send(msg);



        } catch (AuthenticationFailedException ex) {

            request.setAttribute("ErrorMessage", "Authentication failed");



            RequestDispatcher dispatcher = request.getRequestDispatcher(err);

            dispatcher.forward(request, response);

            return;



        } catch (AddressException ex) {

            request.setAttribute("ErrorMessage", "Wrong email address");



            RequestDispatcher dispatcher = request.getRequestDispatcher(err);

            dispatcher.forward(request, response);

            return;

        } catch (MessagingException ex) {

           request.setAttribute("ErrorMessage", ex.getMessage());

       

            RequestDispatcher dispatcher = request.getRequestDispatcher(err);

            dispatcher.forward(request, response);

            return;

        }

            RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp?email="+to);

            dispatcher.forward(request, response);

            return;

    }



    private class SMTPAuthenticator extends Authenticator {



        private PasswordAuthentication authentication;



        public SMTPAuthenticator(String login, String password) {

            authentication = new PasswordAuthentication(login, password);

        }



        protected PasswordAuthentication getPasswordAuthentication() {

            return authentication;

        }

    }



    protected void doPost(HttpServletRequest request,

                         HttpServletResponse response)

                   throws ServletException, IOException {

        processRequest(request, response);

    }



 

}





Comments

Popular posts from this blog

Android App Version Update using the following cordova cli commands

75 inspirational quotes that will change your life

Retrieval Image From DataBase and Display on WebPage by Using Servlets.