Accessing Gmail inbox in GAE with Java

521 views Asked by At

I'm trying to access my Gmail inbox in GAE with Java. I've tried it via IMAP and via POP3. The code for IMAP is the next one:

public class InboxServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(InboxServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
        Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    props.put("mail.imap.host" , "imap.gmail.com");
    props.put("mail.imap.user" , "EMAIL");
    props.put("mail.imap.socketFactory" , 993 );
    props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.imap.port" , 993);
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication( "EMAIL" , "PASSWORD");
                }
    });
    try {
        Store store  = session.getStore("imap");
        store.connect("imap.gmail.com" ,993, "EMAIL" , "PASSWORD");
        Folder fldr = store.getFolder("Inbox");
        fldr.open(Folder.READ_WRITE)
        Message[] ar = fldr.getMessages();
        int count = fldr.getMessageCount();
        resp.getWriter().println(count);
        resp.getWriter().println("<br>");
        resp.getWriter().println(ar[0].getAllRecipients()[0].toString());
        resp.getWriter().println("<br>");
        resp.getWriter().println(ar[0].getFrom()[0].toString());
        resp.getWriter().println("<br>");
        resp.getWriter().println(ar[0].getSentDate().toString());
        resp.getWriter().println("<br>");
        resp.getWriter().println(ar[0].getSubject());
        resp.getWriter().println("<br>");
            ar[0].getContent();
        } catch(Exception exc) {
        resp.getWriter().println(exc + "error");
    }
}
}

With this code I can get everything but the content of the message.

This is the code for POP3:

public class InboxServlet extends HttpServlet {

 private Store store = null;

private static final Logger log = Logger.getLogger(InboxServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws IOException {

    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        Properties pop3Props = new Properties();

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
        pop3Props.setProperty("mail.pop3.port",  "995");
        pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

        URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
                "EMAIL", "PASSWORD");

        Session session = Session.getDefaultInstance(pop3Props , new Authenticator() {
            @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication( "EMAIL" , "PASSWORD");
                    }
        });

             try {
        store = new POP3SSLStore(session, url);
        store.connect();

        Folder fldr = store.getFolder("Inbox");
        fldr.open(Folder.READ_WRITE);
        Message[] ar = fldr.getMessages();
        int count = fldr.getMessageCount();
        resp.getWriter().println(count);
        resp.getWriter().println("<br> ");
            try{
          Object content = ar[1].getContent();
          resp.getWriter().println("From: ");
          resp.getWriter().println(ar[1].getFrom().toString());
          resp.getWriter().println(ar[1].getSubject());
          resp.getWriter().println("<br>");
          resp.getWriter().println("<br>Date: ");
          resp.getWriter().println(prueba.getDescription().toString());
          content = ar[1].getContent();
          resp.getWriter().println("<br> Content: ");
          resp.getWriter().println(content.toString());
        }catch (Error e){
          resp.getWriter().println("error " + e);

        }
        } catch(Exception exc) {
            resp.getWriter().println(exc + "error");
        }
    } 
  }

With this I get only the content of the message, but I can't get the subject, from recipient, date, etc.

Does anybody know how can I do it to get everything with a single system?

Thanks!

0

There are 0 answers