问题
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