我的javamail用imap收邮件出现异常!大家帮忙!!!PS:pop3没问题!

时间:2020-12-11 19:03:07
异常:
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:479)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at msample.Getmail.main(Getmail.java:288)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:364)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:84)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:87)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:446)
... 3 more

主要代码:
public static void main(String args[])throws Exception{//测试
        String host = "imap.126.com";  
        String username ="Taiziq";   
        String password ="cherie";    
        Properties props = System.getProperties();
        props.put("mail.transport.protocol","smtp");
        props.put("mial.store.protocol","imap");
        props.put("mail.smtp.class","com.sun.mail.smtp.SMTPTransport");
        props.put("mail.imap.class","com.sun.mail.imap.IMAPStore");
        props.put("mail.smtp.host",host);
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imap");
        store.connect(host, username, password);
        Folder folder = store.getFolder("INBOX");//收件箱
        folder.open(Folder.READ_ONLY);
        Message message[] = folder.getMessages();
        System.out.println("Messages's length: "+message.length);
        Getmail pmm = null;
        for(int i=0;i<5;i++){
            pmm = new Getmail((MimeMessage)message[i]);
            System.out.println(pmm.getSubject());//主题
        }
    }
麻烦大家帮忙看看,谢谢!

2 个解决方案

#1


拒绝连接,看看你的用户名和密码吧

#2


这个当然没问题啦,都说用pop3没问题,只是改了协议就出问题了!

#1


拒绝连接,看看你的用户名和密码吧

#2


这个当然没问题啦,都说用pop3没问题,只是改了协议就出问题了!