index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action="mail" method="get">

    To:<input type="text" name="to"/><br/>

    Subject:<input type="text" name="subject"><br/>

    Text:<textarea rows="10" cols="70" name="msg"></textarea><br/>

    <input type="submit" value="submit">

    </form>

</body>

</html>

 

Mail.java

public class Mail extends HttpServlet {

  @Override

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

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

 

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

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

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

        out.println(to);

        out.println(subject);

        Mailer.send(to, subject, msg);

        /*String value=Mailer.getValue();*/

        out.print("message has been sent successfully");

        out.close();   

    }

 

   

}

Mailer.java

public static void send(String to,String subject,String msg){

 

        final String user="This email address is being protected from spambots. You need JavaScript enabled to view it. ";//change accordingly

        final String pass="xxxxx";

 

        //1st step) Get the session object

        Properties props = new Properties();

        props.setProperty("mail.smtp.host","localhost");

        /*props.put("mail.smtp.host", "smtp.gmail.com");

         props.put("mail.smtp.port", "27");

        props.put("mail.smtp.auth", "true");*/

 

        Session session = Session.getDefaultInstance(props,

         new javax.mail.Authenticator() {

          protected PasswordAuthentication getPasswordAuthentication() {

           return new PasswordAuthentication(user,pass);

           }

        });

        //2nd step)compose message

        try {

         MimeMessage message = new MimeMessage(session);

         message.setFrom(new InternetAddress(user));

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

         message.setSubject(subject);

         message.setText(msg);

 

         //3rd step)send message

         Transport.send(message);

 

         System.out.println("Done");

 

         } catch (MessagingException e) {

            throw new RuntimeException(e);

         }

    

        }

}

 

 

i include mail.jar and servlet.jar in my STS

when i run this exception occur

exception

javax.servlet.ServletException: Servlet execution threw an exception 

root cause

java.lang.NoClassDefFoundError: javax/mail/MessagingException com.mypack.Mail.doGet(Mail.java:40) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

root cause

java.lang.ClassNotFoundException: javax.mail.MessagingException org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523) com.mypack.Mail.doGet(Mail.java:40) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners