Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When sending mail in java using gmail server error is occurring that "could not connect to smtp host". Below is the code and error:
---------------------------------------------------------------------

Java
package p1;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author abc
 */

public class SendMail
{
   public static boolean sender(String from,String password,String message,String to[])
   {
       String host ="smtp.gmail.com";
       Properties pro = System.getProperties();
       pro.put("mail.smtp.starttls.enable","true");
       pro.put("mail.smtp.host",host);
       pro.put("mail.smtp.user",from);
       pro.put("mail.smtp.password",password);
       pro.put("mail.smtp.port",465);
       pro.put("mail.smtp.auth","true");
       
       Session session = Session.getDefaultInstance(pro,null);
       MimeMessage mimemessage =new MimeMessage(session);
       
       try
       {
           mimemessage.setFrom(new InternetAddress(from));
           //noe get the address of ricipents 
           InternetAddress[] toAddress = new InternetAddress[to.length];
           
           for (int i = 0; i <to.length; i++)
           {
               
               toAddress[i] = new InternetAddress(to[i]);
           }
               //now add the all toaddress elements
               
               for (int j = 0; j <toAddress.length; j++) 
               {
                   
                   mimemessage.addRecipient(RecipientType.TO, toAddress[j]);
               } //add subject
                   mimemessage.setSubject("My name is jay");
                   // set message to mimee message
                   mimemessage.setText(message);
                  
                   Transport transport = session.getTransport("smtp");
                   
                   transport.connect(host,from,password);
                   transport.sendMessage(mimemessage, mimemessage.getAllRecipients());
                       System.out.println("8888888888888");
                   transport.close();
                   
                   
                       
                   return true;
               
       }
            catch(Exception e)// genrate MessagingException
           {
           e.printStackTrace();
           }
       
       return false;
   }
       
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

/**
 *
 * @author abc
 */
public class EmailDemo {
    public static void main(String[] args) {
        String[] to={"jayspatel27@gmail.com","sanjayhpatel3@gmail.com"};
       if( SendMail.sender("jayspatel27@gmail.com", "9426841090", "message to ecipents ", to))
       {
            System.out.println("Success");}
       else
       {
           System.out.println("enter proper detail");
       }
        
        
    }
    
}

------------------------------------------
error:-
---------------------------------------------
C#
run:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
enter proper detail
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
	at javax.mail.Service.connect(Service.java:295)
	at javax.mail.Service.connect(Service.java:176)
	at p1.SendMail.sender(SendMail.java:61)
	at p1.EmailDemo.main(EmailDemo.java:14)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at java.net.Socket.connect(Socket.java:528)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
	... 5 more
BUILD SUCCESSFUL (total time: 22 seconds)
Posted
Comments
Richard MacCutchan 6-Jan-16 6:33am    
This is not a coding issue, it is a problem logging in to gmail. Check your details are valid for this userid.
Jay S Patel 7-Jan-16 1:06am    
email id and password are correct but it gives an error "javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;"
Richard MacCutchan 7-Jan-16 6:17am    
You already told us that, and I explained this is not a coding error. You need to check why you cannot connect to gmail with your userid and password. You can do it fairly easily by using telnet from a command window.

1 solution

Can you try using port 25 as it can be port issue
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900