Com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1; Exception while connecting to gmail in windows remote machine through automation scripts | Selenium Forum
S
sowndarya Posted on 24/02/2020

Hi Team,

In my automation script i have to connect to gmail and validate the received email. Below is the code used to connect to gmail and validate the received email.

 

public static void newemailNotification(String gmailUserName, String gmailPassword,
String sub, String sRid, String rRid,
String reference, String noOfDocs) throws MessagingException, IOException
{
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imaps.partialfetch", "false");
props.put("mail.imap.ssl.enable", "true");
props.put("mail.mime.base64.ignoreerrors", "true");

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", 993, gmailUserName, gmailPassword);

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();

for (Message mail : messages) {
if (!mail.isSet(Flags.Flag.SEEN)) {

String[]mailContent=mail.getContent().toString().split(":");
String subject = mailContent[1];
LOGGER.debug("Subject:" + mailContent[1]);
String ref = mailContent[4];
LOGGER.debug("Reference:" + mailContent[4]);
String doc = mailContent[5].trim();

LOGGER.debug("Actual:" + sub);
LOGGER.debug("Expected:" + subject);

Assert.assertEquals("Actual: " + sub.trim()+" Expected:" + subject.replace("Sender RID/Extension", "").trim(),sub.trim(), subject.replace("Sender RID/Extension", "").trim());

But i am receiving the exception  at the line store.connect("imap.gmail.com", 993, gmailUserName, gmailPassword); as follows

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1;
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:735)
	at javax.mail.Service.connect(Service.java:366)
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:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at java.net.Socket.connect(Socket.java:538)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
	at com.sun.mail.iap.Protocol.<init>(Protocol.java:124)
	at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:128)
	at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:758)
	at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:693)
	at javax.mail.Service.connect(Service.java:366)

Please help me on how to resolve this issue. In local machine the script is running fine but in remote machine i am facing this issue

A
Ashish Thakur Replied on 25/02/2020

Please make sure that no ports are blocked for the connection. May be your firewall is blocking the connection.