Sending mail error, javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

Error is self explainatory: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

You have no SMTP server on localhost, but you configure it there :

  // Assuming you are sending email from localhost
  String host = "localhost";
  ...
  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

So you must:

  • either configure a local SMTP server as a relay on your local system (Postfix or sendmail are two well knows servers)
  • of configure a dummy server that simply traces the mail request but does not even try to deliver mail (Python is known to have such dummy servers out of the box)
  • or configure your application with a server that you are allowed to use - contact your system admin in a corporate environment, or your ISP in an individual one. Anyway, you will need that even to configure a true relay.

You should use the free Google SMTP server as a test.

mail.host=smtp.gmail.com
mail.username=//your gmail
mail.password=//your password
mail.defaultEncoding=UTF-8
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.smtp.socketFactory.port=465

Next, login with your gmail , and turn on less secure apps.


Here is the working solution bro. it's guranteed

1) First of all open your gmail account from which you wanted to send mail, like in you case ""[email protected]"

2) open this link below https://support.google.com/accounts/answer/6010255?hl=en

3) click on "Go to the "Less secure apps" section in My Account." option

4) Then turn on it

5) that's it (: