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.
Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect
As I said, there's nothing wrong with your code. If anything, just to do some testing, try to drop the Authentication part to see if that 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
}
That's the code I'm using every day to send dozens of emails from my application, and it is 100% guaranteed to work -- as long as smtp.gmail.com:587
is reachable, of course.
Got bad greeting from SMTP host: smtp.yandex.ru, port: 465, response: [EOF]] with root cause Yandex
This properties help me
spring:
boot:
admin:
mail:
to: ${spring.mail.username}, test@yandex.ru
from: ${spring.mail.username}
mail:
host: smtp.yandex.ru
username: test@yandex.ru
password: password
port: 587
protocol: smtp
properties:
"mail.transport.protocol": smtp
"mail.smtp.auth": true
"mail.smtp.starttls.enable": true
javax.mail.MessagingException: Could not connect to SMTP host?
This is these two lines which was casting me the problem :
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
and added this line :
m_properties.put("mail.smtp.starttls.enable", "true");
After removing and adding the above lines of code it worked fine.
MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
Fix these common JavaMail mistakes.
Follow the connection debugging tips in the JavaMail FAQ.
Most likely there's a firewall or anti-virus product that's preventing you from connecting.
If Tomcat is running with a Java security manager, the JavaMail FAQ has information about configuring security permissions. If that doesn't help, the JavaMail FAQ also has information about debugging problems with security permissions.
Did I mention that you should read the JavaMail FAQ? :-)
Related Topics
Reading Data from Nested Json Object Using Java
Error: Could Not Find or Load Main Class in Intellij Ide
Cannot Load Driver Class: Com.Mysql.Jdbc.Driver Spring Boot
Multiple Queries Executed in Java in Single Statement
How to Return a Custom Object from a Spring Data JPA Group by Query
Replacing Double Backslashes With Single Backslash
Cannot Find Element Using Selenium Webdriver
Java Lombok: Omitting One Field in @Allargsconstructor
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)
Subscript and Superscript a String in Android
How to Count Method Calls by Instance
How to Find Max Date in List<Object>
How to Read Empty Cells of an Excel File Using Poi
Unable to Deploy Spring Boot 2 Application Due to Hikaripool Exception During Pool Initialization
How to Find the Number of Times If or Else Code Has Executed
How to Link Feature and Step Definition in Cucumber