2

Trying to send an email using java mail api. And I keep getting MailConnectException. I have tried multiple ways to solve it without success.

Exception is thrown by this statement

transport.connect("smtp.gmail.com", "[email protected]", "myPassword"); 

Can anyone tell me what I'm doing wrong?

public static void main(String[] args) { String host = "smtp.gmail.com"; String from = "[email protected]"; Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", "myPassword"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); try{ Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipients(Message.RecipientType.TO, "[email protected]"); message.setSubject("sending in a group"); message.setText("Welcome to JavaMail"); Transport transport = session.getTransport("smtp"); transport.connect("smtp.gmail.com", "[email protected]", "myPassword");//CAUSES EXCEPTION transport.sendMessage(message, message.getAllRecipients()); }catch(MessagingException e){ e.printStackTrace(); } } 

Stack trace:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656) at javax.mail.Service.connect(Service.java:345) at javax.mail.Service.connect(Service.java:226) at com.karmacrafts.util.CustomEmail.main(CustomEmail.java:127) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950) ... 4 more 
1
  • @Caadi0 Thanks for your comment. But I still get the same error. Commented Dec 24, 2013 at 21:27

7 Answers 7

5

This looks like network problem. Even though it could occur due to variety of reasons, like :-

"Couldn't connect to host, port" could be caused by wrong host name, wrong port, a blocking firewall (on the server, on gateways, even on your own machine), network failure, server downtime, etc.

Can you connect to the mail server using telnet ?

Also see this FAQ for some mistakes you committed http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes

Read this answer on how to send emails using gmail https://stackoverflow.com/a/47452/3107043

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

2 Comments

copy pasted the same code from the gmail link you provided with and I still get the same error.
You were correct about the cause possibly being wrong port number I changed the port number to 587 and added this statement to my above code and it worked fine. props.put("mail.smtp.starttls.enable", "true");
1
Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); props.put("mail.smtp.port", "587"); transport.send(message); 

Comments

1

After 8 hours of pain, I've solved this by turning off:

1) my windows firewall

2) my antivirus software

hope this helps

Comments

1

For me, de-activating firewall and anti-virus did not work. I tried the alternate ports given in the mailtrap's SMTP settings 25 or 465 or 587 or 2525

changing spring.mail.port to 2525 in the applications.properties file worked for me

Comments

0

I met the same problem, and the reason is that I opened a vpn. Hope this will be helpful.

Comments

0

Open /etc/postfix/main.cf

Search /inet_interfaces line and comment localhost, un-comment all:

Restart the SMTP server using terminal command "postfix restart"

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

enter image description here

I will try this from two hour.I find out this issue.It is happened when we not properly config our JavaMailSender class.

From application.properties file the spring boot not pick properties. i debug and attatch the image below.Please check this to clear.

username ,host , port are null;

Make this class EmailConfig

@Configuration @Slf4j public class EmailConfig { @Value("${email.smtp-server}") private String host; @Value("${email.port}") private int port; @Value("${email.username}" ) private String username; @Value("${email.password}") private String password; @Bean public JavaMailSender javaMailSender() { log.info("Configuring java email sender :"); JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); log.info("port : {}", port); log.info("Host : {}", host); mailSender.setHost(host); mailSender.setPort(port); mailSender.setUsername(username); mailSender.setPassword(password); Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true"); return mailSender; } } 

in application.yml file add

email: smtp-server: smtp.gmail.com port : 587 username: your-email password: 'your-app-password' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.