javax.mail.MessagingException:尝试使用java mail API访问gmail时重置连接

时间:2012-04-04 10:02:42

标签: javamail connection-reset

我试图使用java mail API读取我的Gmail帐户中的邮件。这是代码:

import java.util.*;
import java.io.*;
import java.awt.*;
import javax.mail.*;
import javax.mail.search.FlagTerm; 
import javax.mail.Flags.Flag;
public class MailPharser {
/**
 * @param args
 */
public void mailRead()
{
    Folder inbox; 
    // TODO Auto-generated method stub
     Properties props = System.getProperties();
     props.setProperty("mail.store.protocol", "imaps"); 
     try
     {
         Session session = Session.getDefaultInstance(props, null); 

         Store store = session.getStore("imaps");

         store.connect("imap.gmail.com","<mailid@gmail.com>", "<password>"); 
         inbox = store.getFolder("Inbox"); 
         System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());


     }
     catch (Exception ex) 
     { System.out.println("Error caught"); ex.printStackTrace(); }

}

public static void main(String[] args) {
    MailPharser mp = new MailPharser();
    mp.mailRead();
}   

}

在跑步时,我收到以下错误:

javax.mail.MessagingException: Connection reset;
  nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at MailPharser.mailRead(MailPharser.java:26)
at MailPharser.main(MailPharser.java:40)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
... 4 more

我添加了跟踪,看到连接调用失败了。我做错了吗?任何帮助都可以。提前谢谢......

1 个答案:

答案 0 :(得分:0)

这可能是与JavaMail无关的网络问题,例如您和Gmail之间的代理或防火墙。

如果您想知道您的代码是否已损坏或网络是否已损坏,您可以test using the code that comes with JavaMail。如果JavaMail代码有效,那么您知道代码有问题,并且可以使用FAQ中的JavaMail代码来改进代码。

如果您发现这是一个网络问题,JavaMail FAQ也有tips for debugging it further