Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Servlet Sending email


The JavaMail API provides many classes that can be used to send email from java. The javax.mail and javax.mail.internet packages contains all the classes required for sending and receiving emails.

For better understanding of this example click steps for sending email from javamail api

For sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

download these jar files or go to the Oracle site to download the latest version.

Example of Sending email through JavaMail API in Servlet

Here is the simple example of sending email from servlet. For this example we are creating 3 files:

  • index.html file for input
  • SendMail.java , a servlet file for handling the request and providing the response to the user. It uses the send method of Mailer class to send the email.
  • Mailer.java , a java class that contains send method to send the emails to the mentioned recipient.

index.html

  1. <form action=“servlet/SendMail”>
  2. To:<input type=“text” name=“to”/><br/>
  3. Subject:<input type=“text” name=“subject”><br/>
  4. Text:<textarea rows=“10” cols=“70” name=“msg”></textarea><br/>
  5. <input type=“submit” value=“send”/>
  6. </form>

SendMail.java

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class SendMail extends HttpServlet {
  8. public void doGet(HttpServletRequest request,
  9.  HttpServletResponse response)
  10.     throws ServletException, IOException {
  11.     response.setContentType(“text/html”);
  12.     PrintWriter out = response.getWriter();
  13.     String to=request.getParameter(“to”);
  14.     String subject=request.getParameter(“subject”);
  15.     String msg=request.getParameter(“msg”);
  16.     Mailer.send(to, subject, msg);
  17.     out.print(“message has been sent successfully”);
  18.     out.close();
  19.     }
  20. }

Mailer.java

  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.InternetAddress;
  4. import javax.mail.internet.MimeMessage;
  5. public class Mailer {
  6. public static void send(String to,String subject,String msg){
  7. final String user=“sonoojaiswal@Kreationnext.com”;//change accordingly
  8. final String pass=“xxxxx”;
  9. //1st step) Get the session object  
  10. Properties props = new Properties();
  11. props.put(“mail.smtp.host”“mail.Kreationnext.com”);//change accordingly
  12. props.put(“mail.smtp.auth”“true”);
  13. Session session = Session.getDefaultInstance(props,
  14.  new javax.mail.Authenticator() {
  15.   protected PasswordAuthentication getPasswordAuthentication() {
  16.    return new PasswordAuthentication(user,pass);
  17.    }
  18. });
  19. //2nd step)compose message
  20. try {
  21.  MimeMessage message = new MimeMessage(session);
  22.  message.setFrom(new InternetAddress(user));
  23.  message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  24.  message.setSubject(subject);
  25.  message.setText(msg);
  26.  //3rd step)send message
  27.  Transport.send(message);
  28.  System.out.println(“Done”);
  29.  } catch (MessagingException e) {
  30.     throw new RuntimeException(e);
  31.  }
  32. }
  33. }

Comments

comments