Search code examples
javaemailjakarta-mailexchange-serverpermission-denied

JavaMail connection problems


I am trying to test a simple program found online to send an email using JavaMail. I am attempting to use a work email but I get an error "Could not connect to SMTP host:..." and "Permission denied: connect". I have looked through other posts on this issue including:

JavaMail Exchange Authentication

Sending email using JSP

JavaMail API to iMail -- java.net.SocketException: Permission denied: connect

I think I have addressed the problems mentioned in the solutions of those posts which are basically the IPv4 issue and the authentication. I am new to attempting to using JavaMail so I wonder if I am making some other beginner mistake. Are there any other things I am overlooking? Is it possible I just do not have access to the server in this manner? I have used generic names not the actual name of my company.

The code is below:

public static void main(String[] args) 
{   
    System.setProperty("java.net.preferIPv4Stack" , "true");
    String host="mail.company.com";   
    final String user="[email protected]";//change accordingly   
    final String password="XXXXXXXX";//change accordingly   

    String to="[email protected]";//change accordingly   

    //Get the session object   
    Properties props = new Properties();   
    props.put("mail.smtp.host",host);   
    props.put("mail.smtp.auth", "true");   

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() 
    {   
        protected PasswordAuthentication getPasswordAuthentication() 
        {   
            return new PasswordAuthentication(user,password);   
        }   
    });   

    //Compose the message   
    try 
    {   
        MimeMessage message = new MimeMessage(session);   
        message.setFrom(new InternetAddress(user));   
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));   
        message.setSubject("javatpoint");   
        message.setText("This is simple program of sending email using JavaMail API");   

        //send the message   
        Transport.send(message);   

        System.out.println("message sent successfully...");   

    } 
    catch (MessagingException e) 
    {
        e.printStackTrace();
    }
}

Stack Trace:

javax.mail.MessagingException: Could not connect to SMTP host: mail.company.com, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at mailtesting.SendMailBySite.main(SendMailBySite.java:45)

Caused by: java.net.SocketException: Permission denied: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more

Solution

  • java.net.SocketException: Permission denied: connect
    

    This means you connected to the server and port and the server actively denied your connection.

    If you try and telnet to this server and port you might get a more descriptive message, either way, that port isn't going to work.

    Most likely that port 25 is blocked as a security measure, this is pretty standard settings for most companies.

    Since you are using Outlook can't find the port in the settings, I am assuming you are connecting to an Exchange server, which might not even have SMTP enabled at all if your entire company is a Microsoft Outlook shop.

    You will need to contact your system admin team to find out what port and protocol you should actually be using.