Failed to connect to the SMTP host: smtp.gmail.com, port: 587; Nested exception: java.net.ConnectException: connection timeout: connect

Here is the application code. I tried to run this using the eclipse IDE. I also added all the necessary java mail jar files, namely dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar,mail.jar . But it gives the following error Could not connect to SMTP host: smtp.gmail.com, port: 587 .

There is no access to blocking the firewall because an answer was received when smtp.gmail.com was called. I even tried it like this:

javax.mail.MessagingException: could not connect to the SMTP host: smtp.gmail.com, port: 587; Nested exception: java.net.ConnectException: connection timeout: connect on com.sun.mail.smtp.SMTPTransport.openServer (SMTPTransport.java:1972) on com.sun.mail.smtp.SMTPTransport.protocolConnect (SMTPTransport.java: 642) in javax.mail.Service.connect (Service.javahaps17) in javax.mail.Service.connect (Service.java:176) in javax.mail.Service.connect (Service.java:125) on javax. mail.Transport.send0 (Transport.java:194) on javax.mail.Transport.send (Transport.java:124) in PlainTextEmailSender.sendPlainTextEmail (PlainTextEmailSender.java:50) in PlainTextEmailSender.main (PlainTextEmailSender.java:73) : java.net.ConnectException: connection timeout: connect on java.net.DualStackPlainSocketImpl.connect0 (native method) on java.net.DualStackPlainSocketImpl.socketConnect (Unknown th source) on java.net.AbstractPlainSocketImpl.doConnect (Unknown source) on java.net.AbstractPlainSocketImpl.connectToAddress (Unknown source) on java.net.AbstractPlainSocketImpl.connect (Unknown source) on java.net.PlainSocketImpl. ) on java.net.SocksSocketImpl.connect (Unknown source) on java.net.Socket.connect (Unknown source) on java.net.Socket.connect (Unknown source) on com.sun.mail.util.SocketFetcher.createSocket ( SocketFetcher.javahaps19) on com.sun.mail.util.SocketFetcher.getSocket (SocketFetcher.java:233) on com.sun.mail.smtp.SMTPTransport.openServer (SMTPTransport.java:1938)

  package net.codejava.mail; 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; public class PlainTextEmailSender { public void sendPlainTextEmail(String host, String port, final String userName, final String password, String toAddress, String subject, String message) throws AddressException, MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // creates a new session with an authenticator Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; Session session = Session.getInstance(properties, auth); // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); // set plain text message msg.setText(message); // sends the e-mail Transport.send(msg); } /** * Test the send e-mail method * */ public static void main(String[] args) { // SMTP server information String host = "smtp.gmail.com"; String port = "587"; String mailFrom = "user_name"; String password = "password"; // outgoing message information String mailTo = "email_address"; String subject = "Hello my friend"; String message = "Hi guy, Hope you are doing well. Duke."; PlainTextEmailSender mailer = new PlainTextEmailSender(); try { mailer.sendPlainTextEmail(host, port, mailFrom, password, mailTo, subject, message); System.out.println("Email sent."); } catch (Exception ex) { System.out.println("Failed to sent email."); ex.printStackTrace(); } } } 
+7
source share
6 answers

As I said, there is nothing wrong with the code. If anything, just to do some testing, try to drop the authentication part to see if this works:

  public void sendPlainTextEmail(String host, String port, final String userName, final String password, String toAddress, String subject, String message) throws AddressException, MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // *** BEGIN CHANGE properties.put("mail.smtp.user", userName); // creates a new session, no Authenticator (will connect() later) Session session = Session.getDefaultInstance(properties); // *** END CHANGE // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); // set plain text message msg.setText(message); // *** BEGIN CHANGE // sends the e-mail Transport t = session.getTransport("smtp"); t.connect(userName, password); t.sendMessage(msg, msg.getAllRecipients()); t.close(); // *** END CHANGE } 

So that the code that I use every day sends dozens of letters from my application, and it works 100% guaranteed - while smtp.gmail.com:587 is available, of course.

0
source

Email went through Gmail using JDK 7 with settings below Gmail .

Go to Gmail Settings Accounts and Import Other Google Account settings and under Sign-in & security

  • 2-Step Verification: Off
  • Allow less secure apps: ON
  • App Passwords: 1 password (16 characters long), later replaced the current password with this.

used for the following maven dependencies:

 spring-core:4.2.2 spring-beans:4.2.2 spring-context:4.2.2 spring-context-support:4.2.2 spring-expression:4.2.2 commons-logging:1.2 <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> 

and my source code:

 import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.swing.JOptionPane; import java.util.List; import java.util.Properties; public class Email { public final void prepareAndSendEmail(String htmlMessage, String toMailId) { final OneMethod oneMethod = new OneMethod(); final List<char[]> resourceList = oneMethod.getValidatorResource(); //Spring Framework JavaMailSenderImplementation JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.gmail.com"); mailSender.setPort(465); //setting username and password mailSender.setUsername(String.valueOf(resourceList.get(0))); mailSender.setPassword(String.valueOf(resourceList.get(1))); //setting Spring JavaMailSenderImpl Properties Properties mailProp = mailSender.getJavaMailProperties(); mailProp.put("mail.transport.protocol", "smtp"); mailProp.put("mail.smtp.auth", "true"); mailProp.put("mail.smtp.starttls.enable", "true"); mailProp.put("mail.smtp.starttls.required", "true"); mailProp.put("mail.debug", "true"); mailProp.put("mail.smtp.ssl.enable", "true"); mailProp.put("mail.smtp.user", String.valueOf(resourceList.get(0))); //preparing Multimedia Message and sending MimeMessage mimeMessage = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setTo(toMailId); helper.setSubject("I achieved the Email with Java 7 and Spring"); helper.setText(htmlMessage, true);//setting the html page and passing argument true for 'text/html' //Checking the internet connection and therefore sending the email if(OneMethod.isNetConnAvailable()) mailSender.send(mimeMessage); else JOptionPane.showMessageDialog(null, "No Internet Connection Found..."); } catch (MessagingException e) { e.printStackTrace(); } } } 

Hope this helps someone.

+1
source

Try this steps

Step 2. Sending mail from your device or application

If you connect using SSL or TLS, you can send mail to anyone using smtp.gmail.com.

Note. Before starting the setup, it is recommended that you configure application passwords for the desired account. Learn more in the “Logging in with Application Passwords” and “Managing User Security Settings” sections.

Connect to smtp.gmail.com through port 465 if you are using SSL. (Connect to port 587 if you are using TLS.) Log in with your Google username and password to authenticate to connect using SSL or TLS. Make sure the username you use clears the CAPTCHA word test that appears the first time you log in.

0
source

AVAST ANTI-VIRUS

An exception in the stream "main" java.lang.RuntimeException: javax.mail.MessagingException: could not connect to the SMTP host: smtp.gmail.com, port: 587; Nested exception: java.net.ConnectException: connection rejected: connection

Desactivar el escudo de correo

0
source

Disable antivirus on your local computer, try as soon as it worked for me

0
source

For all those who are still looking for an answer explained in a simple way, here is the answer:

Step 1: Most antivirus programs block sending email from a computer through an internal application. Therefore, if you receive an error message, you will have to disable the antivirus program when calling these methods / API for sending email.

Step 2. SMTP access to Gmail is disabled by default. To allow an application to send emails using a Gmail account, follow these steps:

  1. Open the link: https://myaccount.google.com/security?pli=1#connectedapps
  2. In the "Security" setting, set "Allow less secure applications to" On .

Step 3: Here is a snippet of code from the code I used, and it works without any problems. From EmailService.java:

 private Session getSession() { //Gmail Host String host = "smtp.gmail.com"; String username = " techdeveloper.aj@gmail.com "; //Enter your Gmail password String password = ""; Properties prop = new Properties(); prop.put("mail.smtp.auth", true); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", host); prop.put("mail.smtp.port", 587); prop.put("mail.smtp.ssl.trust", host); return Session.getInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); } 

I also wrote a blog post and a working application on GitHub with these steps. Please check: http://softwaredevelopercentral.blogspot.com/2019/05/send-email-in-java.html

0
source

All Articles