为什么我会得到这个异常?

发布于 2024-11-25 20:52:26 字数 659 浏览 8 评论 0原文

此方法给出了收件箱中的电子邮件数量。但它给了我这个例外:

javax.mail.MessagingException: Connect failed;
 nested exception is:
java.net.ConnectException: Connection timed out: connecterror

-

 Session session = Session.getInstance(new Properties());
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + "error");
    }    

This method gives the number of emails in the inbox.But it gives me this exception :

javax.mail.MessagingException: Connect failed;
 nested exception is:
java.net.ConnectException: Connection timed out: connecterror

-

 Session session = Session.getInstance(new Properties());
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + "error");
    }    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不吻晚风 2024-12-02 20:52:26

试试这个:

Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , "username");
// Start SSL connection
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.getDefaultInstance(props , new Authenticator() {
    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication( "username" , "password");
            }
});
try {
    Store store  = session.getStore("pop3");
    store.connect("pop.gmail.com" , "username" , "password");
    Folder fldr = store.getFolder("INBOX");
    fldr.open(Folder.HOLDS_MESSAGES);
    int count = fldr.getMessageCount();
    System.out.println(count);
} catch(Exception exc) {
    System.out.println(exc + " error");
}

另请访问此问题

Try this :

Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , "username");
// Start SSL connection
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.getDefaultInstance(props , new Authenticator() {
    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication( "username" , "password");
            }
});
try {
    Store store  = session.getStore("pop3");
    store.connect("pop.gmail.com" , "username" , "password");
    Folder fldr = store.getFolder("INBOX");
    fldr.open(Folder.HOLDS_MESSAGES);
    int count = fldr.getMessageCount();
    System.out.println(count);
} catch(Exception exc) {
    System.out.println(exc + " error");
}

Also visit this question

北方的韩爷 2024-12-02 20:52:26

可能是因为服务器拒绝连接。

尝试从“telnet”连接。一旦可以连接,那么您应该能够从 Java 程序进行连接。

以下是一些故障排除提示:

Probably because the server refuses to connect.

Try connecting from "telnet". Once you can connect at all, then you should be able to connect from your Java program.

Here are some troubleshooting tips:

你怎么敢 2024-12-02 20:52:26

尝试更改

store.connect("pop.gmail.com" , "username" , "password");

store.connect("pop.gmail.com" , 995, "username" , "password");

免责声明:我还没有对此进行测试。

Gmail 需要安全的 SSL 连接,而 javax.mail.Service 可能不提供该连接。不过,我认为更可能的解释是您没有连接到正确的端口,因此我明确为 Gmail 的 POP3 服务指定了正确的端口号。

Try changing

store.connect("pop.gmail.com" , "username" , "password");

to

store.connect("pop.gmail.com" , 995, "username" , "password");

Disclaimer: I have not tested this.

Gmail requires a secure SSL connection, and maybe javax.mail.Service isn't providing that. I think the more likely explanation, though, is that you're simply not connecting to the right port, so I've explicitly specified the correct port number for Gmail's POP3 service.

绅刃 2024-12-02 20:52:26

尝试遵循“如何使用 gmail 作为 smtp 服务器” 教程。 Google 还有一个配置页面,其中包含您需要的所有设置需要。

Try following a "how to use gmail as an smtp server" tutorial. Google also has a configuration page with all the settings you'll need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文