1

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
Harold L. Brown
  • 8,423
  • 11
  • 57
  • 109

4 Answers4

2

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.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
  • firewall issue looks less likely to me (`host: localhost, port: 25`), unless harold performed some changes on local box... and `telnet` was able to connect too. Now on `auth` it's again ruled out as `telnet` client was too didn't use any `auth` info, I missed that and was basing on `exception` @Harold which `smtp` server you have setup on you machine and any specific configuration done?? – harsh Dec 06 '13 at 09:58
  • @harsh I'm running Windows 7 inside a company. The problem occurs with my local Mercury Mail SMTP server as well with a remote test server. – Harold L. Brown Dec 06 '13 at 10:08
  • @harsh The problem was that IPv6 has been blocked. See also http://stackoverflow.com/questions/14064111/java-mail-mystery-smtp-blocked – Harold L. Brown Dec 06 '13 at 11:02
  • Great, I guess `telnet` isn't using `IPv6` and hence worked; a good pointer to remember. – harsh Dec 06 '13 at 11:04
1

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

Aditya
  • 1,334
  • 1
  • 12
  • 23
1

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.

Pritish Shah
  • 611
  • 3
  • 11
  • 25
0

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();
    }
}
harsh
  • 7,502
  • 3
  • 31
  • 32