Cannot force TcpClient to connect to gmail

I am trying to do something that will read email, but I cannot make anything work. This code:

TcpClient c = new TcpClient(); c.Connect("imap.gmail.com", 993); NetworkStream stream = c.GetStream(); stream.ReadTimeout = 1000; stream.ReadByte(); 

The seams should be where any code I load. The last line throws an IOException message with the message: "Unable to read data from the transport connection: the connection attempt failed because the connected party did not respond properly after some time or the connection was not established because the connected host could not respond."

I would agree to a third-party program that automatically downloads email in a format that I can read. I got Thunderbird to connect to gmail, so the problem at my end is for sure.

+4
source share
4 answers

You are connecting to the SSL port. If you do not perform the proper SSL handshake, it will close the connection soon without sending you any data.

0
source

A message indicates that the remote server did not respond. He did not say that "connection is not allowed on this port." He did not say: "I am closing the connection." He said nothing.

This means that you cannot even create a connection at the TCP level. Try the following:

 telnet imap.gmail.com 993 

It will not succeed. Thus, this has nothing to do with your application.

0
source

use openssl to connect to gmail. This is an easy way to achieve your goals.

0
source

Gmail IMAP uses SSL, so you need to bind the network stream using SslStream and call the AuthenticateAsClient method (so that it authenticates the authorization / SSL server authentication). After that, you interact with it as if it is a regular (not SSL) stream:

 TcpClient c = new TcpClient(); c.Connect("imap.gmail.com", 993); SslStream sslStream = new SslStream(c.GetStream()); sslStream.ReadTimeout = 1000; sslStream.AuthenticateAsClient("imap.gmail.com"); sslStream.ReadByte(); 
0
source

All Articles