使用JavaMail API时Gmail Imap与Pop3的对比

使用JavaMail API时Gmail Imap与Pop3的对比,java,gmail,jakarta-mail,gmail-imap,gmail-pop,Java,Gmail,Jakarta Mail,Gmail Imap,Gmail Pop,我正在尝试从gmail帐户的收件箱中提取未读邮件。我写了一个小的演示程序,发现Gmail的pop3在很多情况下都表现得出人意料 当您尝试获取可用文件夹的列表时,Pop3只返回收件箱,而不是所有标签,而IMAP会进行更正。我在这里排队 POP3 public static Result getPop3FolderList() { Properties props = System.getProperties(); props.put("mail.store.protocol",

我正在尝试从gmail帐户的收件箱中提取未读邮件。我写了一个小的演示程序,发现Gmail的pop3在很多情况下都表现得出人意料

  • 当您尝试获取可用文件夹的列表时,Pop3只返回收件箱,而不是所有标签,而IMAP会进行更正。我在这里排队
POP3

public static Result getPop3FolderList()
{
    Properties props = System.getProperties();
    props.put("mail.store.protocol", "pop3s");
    props.put("mail.pop3.host", "pop.gmail.com");     
    props.put("mail.pop3.user", Application.email);
    props.put("mail.pop3.socketFactory", 995);
    props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.pop3.port", 995);

    Session session = Session.getInstance(props,new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(Application.email, Application.pwd);

      }
    });

    try{
        Store store=session.getStore("pop3");
        store.connect(Application.email,Application.pwd);

        javax.mail.Folder[] folders = store.getDefaultFolder().list("*");

        String opHtml = "<ul>";
        for (javax.mail.Folder folder : folders) {
            if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
                opHtml += "<li>" + folder.getFullName()+ "+" + folder.getMessageCount() + "</li>";
            }
        }
        opHtml += "</ul>";
        return ok(opHtml).as("text/html");
    } catch(MessagingException e) {
        return ok("Error in getting list.<br />" + e.getMessage()).as("text/html");
    }
}
public静态结果getPop3FolderList()
{
Properties props=System.getProperties();
props.put(“mail.store.protocol”、“pop3s”);
props.put(“mail.pop3.host”、“pop.gmail.com”);
props.put(“mail.pop3.user”,Application.email);
props.put(“mail.pop3.socketFactory”,995);
put(“mail.pop3.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”);
props.put(“mail.pop3.port”,995);
Session Session=Session.getInstance(props,new Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码验证(Application.email、Application.pwd);
}
});
试一试{
Store Store=session.getStore(“pop3”);
store.connect(Application.email、Application.pwd);
javax.mail.Folder[]folders=store.getDefaultFolder().list(“*”);
字符串opHtml=“
    ”; for(javax.mail.Folder文件夹:文件夹){ if((folder.getType()&javax.mail.folder.HOLDS_邮件)!=0){ opHtml++=“
  • ”+folder.getFullName()+“+”+folder.getMessageCount()+”
  • ”; } } opHtml+=“
”; 返回ok(opHtml).as(“text/html”); }捕获(消息异常e){ 返回ok(“获取列表时出错。
”+e.getMessage()).as(“text/html”); } }

IMAP

public static Result getImapFolderList()
{
  Properties props = System.getProperties();
  props.setProperty("mail.store.protocol", "imaps");
  try {
      Session session = Session.getInstance(props,new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Application.email, Application.pwd);
        }
      });

      javax.mail.Store store = session.getStore("imaps");
      store.connect("imap.gmail.com", Application.email, Application.pwd);
      javax.mail.Folder[] folders = store.getDefaultFolder().list("*");

      String opHtml = "<ul>";
      for (javax.mail.Folder folder : folders) {
          if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
              opHtml += "<li>" + folder.getFullName()+ ":" + folder.getMessageCount() + "</li>";
          }
      }
      opHtml += "</ul>";
      return ok(opHtml).as("text/html");
  } catch (MessagingException e) {
      return ok("Error in getting list.<br />").as("text/html");
  }
}
public静态结果getImapFolderList()
{
Properties props=System.getProperties();
props.setProperty(“mail.store.protocol”、“imaps”);
试一试{
Session Session=Session.getInstance(props,new Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码验证(Application.email、Application.pwd);
}
});
javax.mail.Store Store=session.getStore(“imaps”);
store.connect(“imap.gmail.com”、Application.email、Application.pwd);
javax.mail.Folder[]folders=store.getDefaultFolder().list(“*”);
字符串opHtml=“
    ”; for(javax.mail.Folder文件夹:文件夹){ if((folder.getType()&javax.mail.folder.HOLDS_邮件)!=0){ opHtml+=“
  • ”+folder.getFullName()+”:“+folder.getMessageCount()+”
  • ”; } } opHtml+=“
”; 返回ok(opHtml).as(“text/html”); }捕获(消息异常e){ 返回ok(“获取列表时出错。
”).as(“text/html”); } }

  • 即使在检索邮件时,当我设置未读邮件过滤器时,gmail也会返回大量已读邮件,这些邮件甚至不是收件箱的一部分,而是长期存档的邮件。另一方面,IMAP的行为符合预期
其他信息:我只为新邮件启用了pop3,而不是从一开始就启用了它


我使用的pop3是错误的还是在gmail中坏了?

显然,pop3不处理文件夹。我在访问Exchange邮箱时遇到了同样的问题—IMAP获取文件夹,POP3仅获取收件箱

我在这里找到了更多信息: