Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not connect to SMTP host exception while sending mail through JavaMail API

I am trying to send a email to Gmail account using JavaMail API. I have done the following code.I want to sent mail to multiple recipents.But it is not working.It its giving an exception like "could not connect to SMTP host.sending failed"

package com.cts.email;

import java.util.Properties;


import javax.mail.Message; 
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {     
      Properties emailProperties;  
      Session mailSession;
      MimeMessage emailMessage;   
      public static void main(String args[]) throws MessagingException, javax.mail.MessagingException {      
            SendEmail javaEmail = new SendEmail();  
            Session session=javaEmail.setMailServerProperties();   
            javaEmail.createEmailMessage(session);  
          //  javaEmail.sendEmail(); 
            }    
      public Session setMailServerProperties() {    
          Properties props = new Properties();
        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");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("my_email","my_password");
                }
            });
                return session; 
      }    

            public void createEmailMessage(Session session) throws MessagingException, javax.mail.MessagingException {    
                  String[] toEmails = { "[email protected]","[email protected]" };  
                  try {
                      for (String to_mail : toEmails) {


                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("[email protected]"));
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(to_mail));
                    message.setSubject("Java Email");
                    message.setText("This is an email sent by <b>JavaMail</b> api.");

                    Transport.send(message);



                }
                      System.out.println("Done");
                  }catch (MessagingException e) {
                    throw new RuntimeException(e);
                }   
                }    


                        }

I am getting following exception:

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.cts.email.SendEmail.createEmailMessage(SendEmail.java:62)
    at com.cts.email.SendEmail.main(SendEmail.java:21)
Caused by: javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at com.cts.email.SendEmail.createEmailMessage(SendEmail.java:55)
    ... 1 more

please help me through this.

like image 984
Sandeep Reddy K. Avatar asked Apr 09 '14 07:04

Sandeep Reddy K.


1 Answers

You almost certainly have a firewall or anti-virus program blocking your ability to connect. See the JavaMail FAQ for tips for debugging connection problems.

like image 136
Bill Shannon Avatar answered Oct 23 '22 07:10

Bill Shannon