Ayaz 10/11/2016 0

Accessing a java mail session from a servlet in Java

package com.ack.web.servlet;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * configure a resource reference within the web.xml file
 * that is a container managed javamail session object
 <description>smtp mail out</description>
 * and use your vendor specific xml file to map the
 * resource name to the jndi name, eg for weblogic

public class AccessingAJavaMailSessionFromAServlet extends HttpServlet {
  public void doGet( HttpServletRequest req, HttpServletResponse res )
      throws ServletException, IOException {
    res.setContentType( "text/html" );
    PrintWriter pw = res.getWriter();

    try {
      // get hold of the JavaMail session
      Context ctx = new InitialContext();
      Session mailSession = (Session) ctx.lookup( "java:comp/env/mail/smtp" );

      // create a message
      Message msg = new MimeMessage( mailSession );
      msg.setSubject( "a servlet test email" );
      msg.setSentDate( new java.util.Date( System.currentTimeMillis() ) );
      msg.setText( "<h1>Hello You</h1>" );
      msg.addHeader( "Content-Type", "text/html" );
      msg.setFrom( new InternetAddress( "x@xxx" ) );
      msg.setRecipient( Message.RecipientType.TO,
                        new InternetAddress( "cleve" ) );

      // send it
      Transport.send( msg );

      // and provide feedback to the use
      pw.println( "message sent!" );
    catch( Exception ex ) {
      // if we get a problem, log it
      log( "problem sending message", ex );

      // and send an error back to the client
      res.sendError( res.SC_INTERNAL_SERVER_ERROR, ex.getMessage() );

Report Bug

Please Login to Report Bug

Reported Bugs


Please Login to Comment