javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 Can not manage to solve this problem

could not connect to smtp host smtp.gmail.com port 465 jenkins
could not connect to smtp host: smtp-mail.outlook.com, port: 587
javax mail messagingexception could not connect to smtp host connection timed out
could not connect to smtp host "response -1"
could not connect to smtp host java
javax mail messagingexception could not connect to smtp host smtp mail yahoo com port: 465, response
javax mail messagingexception unknown smtp host
could not connect to smtp host port: 25, response: 421

I'm trying to send an email in Android Studio without intents and to achieve this I'm trying to use the JavaMail API. However I got an error every time I tried to send an email:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587

I have already implemented activation.jar, additional.jar and mail.jar to my project. Also, I gave permission in the Manifest file for INTERNET. I also gave permission for lower security access on my Gmail account.

Here's my code:

public class MailSend extends AsyncTask {

//Declaring Variables
private Context context;
private Session session;

//Information to send email
private String email;
private String subject;
private String message;

//Progressdialog to show while sending email
private ProgressDialog progressDialog;

//Class Constructor
public MailSend(Context context, String email, String subject, String message) {
    //Initializing variables
    this.context = context;
    this.email = email;
    this.subject = subject;
    this.message = message;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
    progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    progressDialog.dismiss();
    //Showing a success message
    Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {
    final String username = "user";
    final String password = "pass";

    Properties props = System.getProperties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("user", "pass");
                }
            });

    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

        //Setting sender address
        mm.setFrom(new InternetAddress("user"));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(message);

        //Sending email
        Transport.send(mm);

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

    return null;
}

}

Could anyone please tell me if my code is wrong or am I missing something? Here's the full error output.

W/System.err: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
                nested exception is:
W/System.err: 	java.net.ConnectException: failed to connect to smtp.gmail.com/64.233.184.108 (port 465) from /:: (port 56438): connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
W/System.err:     at javax.mail.Service.connect(Service.java:310)
                  at javax.mail.Service.connect(Service.java:169)
W/System.err:     at javax.mail.Service.connect(Service.java:118)
W/System.err:     at javax.mail.Transport.send0(Transport.java:188)
W/System.err:     at javax.mail.Transport.send(Transport.java:118)
                  at com.example.meric.knockknockapp.MailSend.doInBackground(MailSend.java:105)
                  at com.example.meric.knockknockapp.MailSend.doInBackground(MailSend.java:31)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                  at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: java.net.ConnectException: failed to connect to smtp.gmail.com/64.233.184.108 (port 465) from /:: (port 56438): connect failed: ETIMEDOUT (Connection timed out)
                  at libcore.io.IoBridge.connect(IoBridge.java:138)
W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
                  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
                  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
                  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
                  at java.net.Socket.connect(Socket.java:616)
                  at java.net.Socket.connect(Socket.java:565)
W/System.err:     at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
                  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
              	... 14 more
W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at libcore.io.Linux.connect(Native Method)
                  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:126)
W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:152)
                  at libcore.io.IoBridge.connect(IoBridge.java:130)

Change Properties to this:-

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465")

If didn't work

Check this Link:-Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

Hope this will Help!!!!

Error: Could not connect to SMTP host: smtp.gmail.com, port: 465 , Hi, I tried to creating Alarm and sending email. Mail server connection failed; nested exception is javax.mail to host and review code, manage projects, and build software together. Error: Could not connect to SMTP host: smtp.gmail.com​, port: 465, I solved this issue. So i changed port to 587. javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587. I have already implemented activation.jar, additional.jar and mail.jar to my project. Also, I gave permission in the Manifest file for INTERNET. I also gave permission for lower security access on my Gmail account. Here's my code:


This Code works for me:-

public class SendMail extends AsyncTask<Void, Void, Void> {

//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email="", femail="", fpass="";
private String subject="";
private String message="";

//Class Constructor
public SendMail(Context context, String fromemail, String frompass, String toemail, String subject, String message) {
    //Initializing variables
    this.context = context;
    this.email = toemail;
    this.femail = fromemail;
    this.fpass = frompass;
    this.subject = subject;
    this.message = message;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Showing a success message
    Toast.makeText(context, "Send to "+email+" from "+femail+".", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {
    //Creating properties
    Properties props = new Properties();

    //Configuring properties for gmail
    //If you are not using gmail you may need to change the values
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(femail, fpass);
                }
            });
    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);
        //Setting sender address
        mm.setFrom(new InternetAddress(femail));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(message);           
        //Sending email
        Transport.send(mm);

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

}

Getting Error in Sending Javamail Using smtp.gmail.com With Ports , SendFailedException: Sending failed; nested exception is: javax.mail.​MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465 at javax.mail. I'm getting below error when I send mail using javamail smtp.​gmail.com port: 25, 465 or 587. If you find some solution to this problem please mail me. possible duplicate of Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 – bummi Oct 25 '14 at 18:50 1 It is not a duplicate because none of the answers in the question you mention solve my problem.


The code that I have posted above works completely fine. The issue was that I was on a public network, which seems to cause of the problem. When I switch to a private network the problem is gone. Thanks everyone.

JavaMail API - Java program to send email, Learn to send email using gmail smtp to multiple recipients using Java Mail API. Given below is program which can be used to send emails using Gmail SMTP server. Love computers, programming and solving everyday problems. MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 586​; It is very clear from your exception that it is trying to connect to localhost and not to 10.101.3.229. exception snippet : Could not connect to SMTP host: localhost, port: 25; 1.) Please check if there are any null check which is setting localhost as default value. 2.)


"cannot connect to smtp.gmail.com, port 587: Connection timed out , Hi, I tried this tutorial in order to enabling the email sending facility to the "​cannot connect to smtp.gmail.com, port 587: Connection timed out" Jul 25 06:​39:08 host=smtp.gmail.com tls=on auth=on user=aleem.firnas22gmail.com If you run into issues leave a comment, or add your own answer to help  Am using token SMTP, host smtp.gmail.com, port 465 and authentication and TLS are enable true. So, help me how to solve this issue. Your quick answer is so helpful for me.


Caught javax.mail.MessagingException: Could not connect to SMTP , MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465 . "Directory does not exist" Question Purge of data in non work item table Question Pega 8.4.1and8.4.2 Case Management System Administration STARTTLS is enabled on port 587 of the gmail server. when using port 465 for the gmail  Hi All, The thing is that when using the Jaba Mail API for sending mails using smtp protocol, the default protocol for smtp is 25. But , this is not going to be the case in most of the smtp ptotocols.


Mail Server Troubleshooting :: ManageEngine OpManager, For example: telnet smtp.gmail.com 587; If the mail server ports are blocked on Could not connect to SMTP host: xxxx, port: yyyy , response: -1 530 5.7.0 Must issue a STARTTLS command first. javax.mail.MessagingException: Could not convert socket to TLS; SSLHandshakeException cannot be cast to javax .mail. Gmail SMTP server – smtp.gmail.com; Port – 465 (SSL required); Port – 587 (TLS required); Use Authentication – Yes; 2. Java program to send email though Gmail server. Given below is program which can be used to send emails using Gmail SMTP server.