1

我正在尝试使用 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");
        }
    } 
  }

有了这个,我只能得到消息的内容,但我无法从收件人、日期等处获取主题。

有谁知道我怎样才能用一个系统获得所有东西?

谢谢!

4

0 回答 0