JavaMail:如何解决SocketException?

发布于 2024-12-23 14:57:04 字数 1248 浏览 2 评论 0原文

我使用以下代码从 Java 应用程序发送电子邮件。在我的计算机上它工作正常,在第二台计算机上也工作正常,但在另一台计算机(在同一网络中)上却不能,尽管我们使用相同的连接设置。

public void connect() throws MessagingException
{
    Authenticator auth = new Authenticator(){ 
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(smtpUser, smtpPassword);
        }
    };
    Properties props = new Properties();
    props.put("mail.smtp.host", this.smtpServer);
    props.put("mail.smtp.auth", true); 

    session = Session.getDefaultInstance(props, auth);

    store = session.getStore(this.protocol);
    store.connect(this.mailboxServer, this.user, this.password);  //<-- exception is thrown here

}

这是我得到的异常:

javax.mail.MessagingException: connect failed; 
nested exception is: 
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161)
    at javax.mail.Service.connect(Service.java:288)
    at javax.mail.Service.connect(Service.java:169)
    at com.myapp.MailboxConnection.connect(MailboxConnection.java:66)
caused by: java.net.SocketException: Permission denied: connect

它无法在一台计算机上运行的原因可能是什么?

I am using the following code to send emails from a Java application. On my computer it works fine, on second computer too, but on another computer (in the same network) it doesn't, although we're using the same connection settings.

public void connect() throws MessagingException
{
    Authenticator auth = new Authenticator(){ 
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(smtpUser, smtpPassword);
        }
    };
    Properties props = new Properties();
    props.put("mail.smtp.host", this.smtpServer);
    props.put("mail.smtp.auth", true); 

    session = Session.getDefaultInstance(props, auth);

    store = session.getStore(this.protocol);
    store.connect(this.mailboxServer, this.user, this.password);  //<-- exception is thrown here

}

This is the exception I get:

javax.mail.MessagingException: connect failed; 
nested exception is: 
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161)
    at javax.mail.Service.connect(Service.java:288)
    at javax.mail.Service.connect(Service.java:169)
    at com.myapp.MailboxConnection.connect(MailboxConnection.java:66)
caused by: java.net.SocketException: Permission denied: connect

What could be the reason that it doesn't work on one computer?

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

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

发布评论

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

评论(4

烟织青萝梦 2024-12-30 14:57:04

您应该检查使用了哪个端口,也许它是您机器上允许的端口,但在另一台机器上不允许。

You should check which port is used, maybe it is an allowed port on your machine, but not on the other.

三月梨花 2024-12-30 14:57:04

我认为这可能是网络问题或者可能是权限问题(更改您的用户权限为管理员)

检查您的系统是否能够连接到邮件服务器

ping mailservername/mailserverip 例如:ping www.gmail.com/192.168.1.98

i think this may be a network problem or may be a permission problem(change your user privilege as administrator)

check whether your system would be able to connect to the mail server

ping mailservername/mailserverip eg: ping www.gmail.com/192.168.1.98

好菇凉咱不稀罕他 2024-12-30 14:57:04

您的代码是否在启用了安全管理器的应用程序服务器中运行? Java 运行时可能未授予您的应用程序连接到该主机的权限。

Is your code running in an application server with a security manager enabled? Your application may not have been given permission by the Java runtime to connect to that host.

白云悠悠 2024-12-30 14:57:04

通过卸载 Norton AntiVirus 解决问题

problem solved by uninstalling Norton AntiVirus

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