无法通过IMAP连接到电子邮件

时间:2019-05-22 14:25:01

标签: java gmail

我在PC上创建了一个程序,该程序通过IMAP连接到Gmail,并检查文件夹中是否有新邮件。该程序正常工作,但是在另一台PC上出现以下错误: 编辑:

    Exception in thread "main" javax.mail.MessagingException: Connection refused: connect;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:618)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at test.Oznameni.doit(Oznameni.java:20)
    at test.Oznameni.main(Oznameni.java:153)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:109)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:585)
    ... 4 more

但是我感觉它显示与Gmail的IMAP的连接错误。 谢谢。 我附上我的代码:

private static void doit() throws Exception {
        Folder folder = null;
        Store store = null;
        try {
            Properties props = System.getProperties();
            props.setProperty("mail.store.protocol", "imaps");
            Session session = Session.getDefaultInstance(props, null);
            store = session.getStore("imaps");
            store.connect("imap.gmail.com","xxxx@gmail.com", "xxxxx");
            folder = store.getFolder("xxxxx");
            folder.open(Folder.READ_WRITE);
            Message messages[] = folder.getMessages();
            int unread = folder.getUnreadMessageCount();
            if (unread > 0){
                Message msg = messages[0];
                String from = msg.getFrom()[0].toString();
                String subject = msg.getSubject();
                if(from.equals("xxxxxx") && subject.equals("xxxxx")){ 
                    noti();
                    msg.setFlag(Flags.Flag.DELETED, true);
                }
            }
        }
        finally {
            if (folder != null) { folder.close(true); }
            if (store != null) { store.close(); }
        }
        TimeUnit.MINUTES.sleep(15);
        doit();
    }

0 个答案:

没有答案