JavaMail: How to solve SocketException?

。_饼干妹妹 提交于 2019-12-11 16:08:30

问题


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?


回答1:


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




回答2:


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




回答3:


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.




回答4:


problem solved by uninstalling Norton AntiVirus



来源:https://stackoverflow.com/questions/8671319/javamail-how-to-solve-socketexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!