1

I have tried to send a simple e-mail to any gmail account using Gmail SMTP. getting the below error.

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

My code is

package common;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPMessage;

public class SimpleMail{

    /**
           Outgoing Mail (SMTP) Server
           requires TLS or SSL: smtp.gmail.com (use authentication)
           Use Authentication: Yes
           Port for SSL: 465
         */
        public static void main(String[] args) {

             String to="[email protected]";
             String subject="New Mail";

             String msg="test test";

            final String user="gmailuser";
            final String pass="gmailpassowd";
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587"); //this is optional
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");



            Session session = Session.getInstance(props,new javax.mail.Authenticator() {

                    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,pass);
           }
           });

            try {
           MimeMessage message = new MimeMessage(session);
           message.setFrom(new InternetAddress(user));
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
           message.setSubject(subject);
           message.setText(msg);

             Transport.send(message);
                System.out.println("Mail sent..");
       }catch(Exception e)
       {
           System.out.println(e);
       }
    }



}
6
  • 1
    Is your internet connection working properly? Are you using some proxy? Commented Apr 23, 2015 at 10:06
  • I think you should try the Gmail via SSL example from mkyong.com/java/…. Also, your recipient's email address (if its gmail) should have this setting otherwise the mail does not get sent - Gmail security setting->Account permission->Access for less secure apps Commented Apr 23, 2015 at 10:19
  • java.net.ConnectException: Connection timed out: connect means you have some internet and or firewall issues Commented Apr 23, 2015 at 10:22
  • 1
    Turn off your firewall and try Commented Apr 23, 2015 at 10:30
  • firewall is turned off already Commented Apr 23, 2015 at 11:53

2 Answers 2

1

Check your ports. From Google's support:

If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL).

Source: https://support.google.com/mail/answer/78775?hl=en

So, try using port 25 and see what happens.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried with SSL with port 25 and also access less secure apps is allowed. getting below error,
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
Can you ping smtp.gmail.com ?
Pinging gmail-smtp-msa.l.google.com [74.125.200.108] with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 74.125.200.108: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
There's your problem. Check with your network admin.
1

I have sent emails through the Gmail smtp in java using the Apache Commons Email library.
The documentation has a nice and simple example:

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.