1

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="user@company.com";//change accordingly   
    final String password="XXXXXXXX";//change accordingly   

    String to="user@company.com";//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
Community
  • 1
  • 1
klib
  • 697
  • 2
  • 11
  • 27
  • I tried ping mail.company.com command in DOS prompt and looks like email service is up and running. Looking at message you have not provided correct authentication hence doesn't allow you to connect. – Mind Peace Jul 24 '14 at 18:08
  • Have you tried setting session debug on? That should provide some added info. – rrirower Jul 24 '14 at 18:10
  • What is the exact error? Please provide the full stack trace. – Bart Jul 24 '14 at 18:13
  • If you are still getting a SocketException, my guess is that the target mail server does not allow connections on port 25 (the default port for unencrypted SMTP). Modern servers encrypt e-mail transmission. Try adding `props.put("mail.smtp.port", "587");` and `props.put("mail.smtp.starttls.enable", "true");`. – VGR Jul 24 '14 at 18:15
  • @MindPeace I'm sorry I used generic names for the service thats not actually what I am connecting to – klib Jul 24 '14 at 18:20
  • @VGR That could be the problem. I tried both of those and I still get the same error. Is it possible it is a different port and if so how would I find out? Do I need to find the administrator? – klib Jul 24 '14 at 18:30
  • @rrirower I tried setting the debg on but I did not get anything else – klib Jul 24 '14 at 18:33
  • 1
    It is probably a good to ask the system administrator, yes. If you are using that server for your own e-mail, you may be able to find the port in your own mail client's account settings (Thunderbird, Outlook, etc.). – VGR Jul 24 '14 at 18:35
  • That would be very helpful since tracking down the system administrator is going to be a quest. You have no idea what I am dealing with. I am using outlook, do you know where I can find it? I cannot find it in account settings. I found the server but not the port. – klib Jul 24 '14 at 18:40

1 Answers1

4
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.

Community
  • 1
  • 1