尝试使用java在gmail中搜索主题为的邮件

2vuwiymt  于 2021-05-29  发布在  Java
关注(0)|答案(0)|浏览(252)

我公司的邮件域是gmail,我需要用主题行搜索邮件。我写了下面的代码,但gmail没有连接正确的用户名和密码。

public static void getMail() {
                 Scanner sc = new Scanner(System.in);
                 final String m10 = "abc@abc.com";   
                 final String n10 = "abcd";
                 String host = "absmtp.abc.com"; 
                 try
                 {
                 Properties pro1 = new Properties();   
                 pro1.put("mail.smtp.host", host);    
                 pro1.put("mail.smtp.socketFactory.port", "465");
                 MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
                    socketFactory.setTrustAllHosts(true);
                    pro1.put("mail.imaps.ssl.socketFactory", socketFactory);
              //   pro1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
               //  pro1.put("mail.smtp.auth", "true");   
                 pro1.put("mail.smtp.port", "465");  
             Session session = Session.getDefaultInstance(pro1); 
                 Store store = session.getStore("imaps");  
                 store.connect(host, m10, n10); 
                 Folder folderbox = store.getFolder("INBOX");
                 folderbox.open(Folder.READ_ONLY);   
                 SearchTerm search = new SearchTerm(){

                     @Override
                     public boolean match(Message message) {
                         try
                         {
                             if(message.getSubject().contains("") ) 
                             {
                                 return true;
                             }
                         }
                         catch(Exception e)
                         {
                             System.err.println(e.getMessage());
                         }
                         return false;
                     }

                 };

                 Message[] found = folderbox.search(search);

                 int length = found.length;
                 for(int i = 0;i<found.length;i++)
                 {
                     Message mess1 = found[i];
                     System.out.println("->Message Number > "+i);
                     System.out.println("->Message Subject >"+mess1.getSubject());
                 }
                 folderbox.close(true);
                 store.close();
             }
                 catch(Exception e)
                 {
                     System.err.println(e.getMessage());
                 }

               }

未连接并给出错误:
无法连接到主机,端口:@.com, 993; 超时-1。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题