Mail Accessor : Email « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » Email 




Mail Accessor

import java.io.IOException;
import java.io.PrintWriter;
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;

import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;

public class MailAccessor extends HttpServlet {

  private final static String DEFAULT_SERVER = "mail.attbi.com";

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

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

    handleMessages(request, out);

    out.println("</body></html>");
    out.close();
  //doGet

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

    HttpSession httpSession = request.getSession();
    String user = (StringhttpSession.getAttribute("user");
    String password = (StringhttpSession.getAttribute("pass");
    String popAddr = (StringhttpSession.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.");

      folder.open(Folder.READ_ONLY);

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

      if (msgLen == 0)
        out
            .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) {

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

    finally {
      try {
        if (folder != null)
          folder.close(false);

        if (popStore != null)
          popStore.close();
      catch (Exception e) {
      }
    }
  }//printMessages

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

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

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

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

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

    }

  }//displayMessage

  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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.