Mail Accessor : Email « Servlets « Java

Mail Accessor

import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MailAccessor extends HttpServlet {

  private final static String DEFAULT_SERVER = "";

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

    response.setContentType("text/html"); out = response.getWriter();
    out.println("<html><head><title>Email Reader</title></head><body>");

    handleMessages(request, out);

  } //doGet

  private void handleMessages(HttpServletRequest request, PrintWriter out)
      throws IOException, ServletException {

    HttpSession httpSession = request.getSession();
    String user = (String) httpSession.getAttribute("user");
    String password = (String) httpSession.getAttribute("pass");
    String popAddr = (String) httpSession.getAttribute("pop");

    Store popStore = null;
    Folder folder = null;

    if (!check(popAddr))
      popAddr = MailAccessor.DEFAULT_SERVER;

    try {

      if ((!check(user)) || (!check(password)))
        throw new ServletException(
            "A valid username and password is required to check email.");

      Properties properties = System.getProperties();

      Session session = Session.getDefaultInstance(properties);

      popStore = session.getStore("pop3");

      popStore.connect(popAddr, user, password);

      folder = popStore.getFolder("INBOX");

      if (!folder.exists())
        throw new ServletException(
            "An 'INBOX' folder does not exist for the user.");;

      Message[] messages = folder.getMessages();
      int msgLen = messages.length;

      if (msgLen == 0)
            .println("<h2>The INBOX folder does not yet contain any email messages.</h2>");

      for (int i = 0; i < msgLen; i++) {
        displayMessage(messages[i], out);
        out.println("<br /><br />");

    } catch (Exception exc) {

          .println("<h2>Sorry, an error occurred while accessing the email messages.</h2>");

    } finally {
      try {
        if (folder != null)

        if (popStore != null)
      } catch (Exception e) {

  private void displayMessage(Message msg, PrintWriter out)
      throws MessagingException, IOException {

    if (msg != null && msg.getContent() instanceof String) {

      if (msg.getFrom()[0] instanceof InternetAddress) {
        out.println("Message received from: "
            + ((InternetAddress) msg.getFrom()[0]).getAddress()
            + "<br />");

      out.println("Message content type: " + msg.getContentType()
          + "<br />");
      out.println("Message body content: " + (String) msg.getContent());
    } else {

          .println("<h2>The received email message was not of a text content type.</h2>");



  private boolean check(String value) {

    if (value == null || value.equals(""))
      return false;

    return true;

Related examples in the same category

1.Sending E-Mail from Servlets
2.Java servlet application
3.Email JNDI Filter
4.Email Bean
5.Servlet and email
6.Sending e-mail by using the JavaMail API and JDBC