Choose Your Language

Sunday, 14 April 2013

web application to send mail using jsp and servlet

sendmail.jsp
<%--
    Document   : sendmail
    Created on : 10 Apr, 2013, 8:49:08 PM
    Author     : Aravind Sankaran
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post" action="/applicationcontext/MailSender">
                                <table>
                                    <tr>
                                        <td></td>
                                        <td>gmail Id</td>
                                        <td><input type="text" name="gmailId" style="width: 418px"></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>gmail Password</td>
                                        <td>

                                        <input type="password" name="gmailPassword" style="width:  418px"></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>To</td>
                                        <td><input type="text" name="to" style="width: 418px"></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>Subject</td>
                                        <td><input type="text" name="subject" style="width: 418px"></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>Body</td>
                                        <td><textarea rows="3" cols="50" name="body"></textarea></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td align="center"><input type="submit" value="Send Mail"></td>
                                        <td></td>
                                    </tr>
                                </table>
                                </form>

    </body>
</html>

MailSender.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Aravind Sankaran
 */
public class MailSender extends HttpServlet {

   
    private static final String SMTP_HOST_NAME = "smtp.gmail.com"; 
    private static final String SMTP_PORT = "465"; 
    final  String password="";
     final String from="";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }



    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {               

        
         String gmailId = request.getParameter("gmailId");

         String gmailPassword=request.getParameter("gmailPassword");
         String toEmailAddress = request.getParameter("to");

         String toEmail[]={toEmailAddress};
         String emailSubject = request.getParameter("subject");

         String emailBody =request.getParameter("body");
         MailSender mailer=new MailSender();
        try {
            mailer.sendSSLMessage(toEmail, emailSubject,emailBody , gmailId, gmailPassword);
        } catch (MessagingException ex) {
            Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

public void sendSSLMessage(String recipients[], String subject, 
            String message, final String from, final String password) throws MessagingException

        boolean debug = true; 
        Properties props = new Properties(); 
        props.put("mail.smtp.host", SMTP_HOST_NAME); 
        props.put("mail.smtp.auth", "true"); 
        props.put("mail.debug", "true"); 
        props.put("mail.smtp.port", SMTP_PORT); 
        props.put("mail.smtp.socketFactory.port", SMTP_PORT); 
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY); 
        props.put("mail.smtp.socketFactory.fallback", "false"); 
 
        Session session = Session.getDefaultInstance(props, 
                new javax.mail.Authenticator() { 
                    protected PasswordAuthentication getPasswordAuthentication() { 
                        return new PasswordAuthentication(from, 
                                password); 
                    } 
                });
 
        session.setDebug(debug); 
 
        Message msg = new MimeMessage(session); 
        InternetAddress addressFrom = new InternetAddress(from); 
        msg.setFrom(addressFrom); 
 
        InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
        for (int i = 0; i < recipients.length; i++) { 
            addressTo[i] = new InternetAddress(recipients[i]); 
        } 
        msg.setRecipients(Message.RecipientType.TO, addressTo); 
 
        msg.setSubject(subject); 
        msg.setContent(message, "text/plain"); 
        Transport.send(msg); 
    } 

 
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

 

web.xml
<web-app>
<servlet>
        <servlet-name>MailSender</servlet-name>
        <servlet-class>commons.MailSender</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>MailSender</servlet-name>
        <url-pattern>/MailSender</url-pattern>
</servlet-mapping>
<session-config>
        <session-timeout>
            30
        </session-timeout>
</session-config>
<welcome-file-list>
        <welcome-file>sendmail.jsp</welcome-file>
</welcome-file-list>
</web-app>

jar file needed:
java-mail-1.4.4.jar





No comments:

Post a Comment