Question

I want to send an email via Java but unfortunately I'm getting the following exception:

javax.mail.MessagingException: 
    Could not connect to SMTP host: localhost, port: 25;
        nested exception is:
            java.net.SocketException: Permission denied: connect

I'm using this little example program to send my email.

However when trying to connect to the server with

telnet localhost 25

I'm able to establish a connection.

The same situations occurs with a remote SMTP server.

What could be the problem?


Here the output with mail.debug set to "true":

DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
Was it helpful?

Solution

I don't agree with harsh (sorry, can't comment where i should, not enough reputation). The Permission denied error happens within the connect system call, so it's way before the client can even try to authenticate to the server.

Unfortunately, you don't provide any information about which system you're running on - windows, linux, some other unix or something completely different.

I had this kind of problem once in a company where the local McAffee on all client PCs was configured to prevent all traffic to port 25, so if a client caught a virus, it still wouldn't be able to mailbomb others. Maybe something like that is running on your system?

Or, maybe it's a problem with ipv4 (which i assume you're using from java) being blocked, but ipv6 allowed. Try do do a netstat after telnet connects to find out which one it's using. But again, i'd assume it has to do with the firewall settings on your computer, which might allow one of the IP protocols but not the other.

OTHER TIPS

For sending the mail through your application via SMTP you need the IP of the machine to be relayed on the SMTP server.

Get the Ip relayed on SMTP server. Henceforth, you will be able to send the mails and wont get the exception as Permission denied

You may try following program to check with gmail smtp server (assuming you have gmail account), if this works fine surely you have some issue with Mercury mail server setup:

public static void main(String[] args) throws Exception {
    String host = "smtp.gmail.com";
    String username = "your-gmail-username@gmail.com";
    String password = "gmail-password";
    Properties props = new Properties();
    props.put("mail.debug", true);
    Session session = Session.getInstance(props);
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(username));
    msg.setRecipient(RecipientType.TO, new InternetAddress(username));
    msg.setSubject("This is the Subject Line!");
    msg.setContent("<h1>This is actual message</h1>",
                       "text/html" );
    Transport t = session.getTransport("smtps");
    try {
        t.connect(host, username, password);
        t.sendMessage(msg, msg.getAllRecipients());
    } finally {
        t.close();
    }
}

I had the similar issue. After spending few hours, finally resolved issue by putting below line in code:

System.setProperty("java.net.preferIPv4Stack" , "true");

By default, Java uses ipv6, and I was passing ipv4 address. After putting above line, It worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top