JavaMail:如何解决SocketException?
我使用以下代码从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该检查使用了哪个端口,也许它是您机器上允许的端口,但在另一台机器上不允许。
You should check which port is used, maybe it is an allowed port on your machine, but not on the other.
我认为这可能是网络问题或者可能是权限问题(更改您的用户权限为管理员)
检查您的系统是否能够连接到邮件服务器
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
您的代码是否在启用了安全管理器的应用程序服务器中运行? 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.
通过卸载 Norton AntiVirus 解决问题
problem solved by uninstalling Norton AntiVirus