使用Java访问GAE中的Gmail收件箱

时间:2013-07-17 13:05:55

标签: java google-app-engine gmail gmail-imap gmail-pop

我正在尝试使用Java访问GAE中的Gmail收件箱。我通过IMAP和POP3尝试过。 IMAP的代码是下一个:

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");
    }
}
}

使用此代码,我可以获得除消息内容之外的所有内容。

这是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");
        }
    } 
  }

有了这个,我只得到了邮件的内容,但我无法从收件人,约会对象中获得主题。

有人知道我怎样才能通过单一系统获取所有内容?

谢谢!

0 个答案:

没有答案