Connecting to smtp.gmail.com via command line

Abhishek picture Abhishek · Oct 4, 2009 · Viewed 142.4k times · Source

I am in the process of writing an application that sends mail via an valid GMail user ID and password.

I just wanted to simulate the SMTP connection on my Windows XP command line, and when I telnet smtp.gmail.com at 465 port - I don't see any thing. A blank command window with title Telnet smtp.gmail.com opens with cursor. When I type in EHLO or usual SMTP handshake commands, the prompt just closes.

I am unable to figure out whats going wrong and where. I tried connecting to 587, it does not connect in telnet at all. Could anyone please clarify if I am doing something wrong?

Answer

the911s picture the911s · Dec 8, 2012

Using Linux or OSx, do what Sorin recommended but use port 465 instead. 25 is the generic SMTP port, but not what GMail uses. Also, I don't believe you want to use -starttls smtp

openssl s_client -connect smtp.gmail.com:465

You should get lots of information on the SSL session and the response:

220 mx.google.com ...

Type in

HELO smtp.gmail.com 

and you'll receive:

250 mx.google.com at your service

From there it is not quite as straightforward as just sending SMTP messages because Gmail has protections in place to ensure you only send emails appearing to be from accounts that actually belong to you. Instead of typing in "Helo", use "Ehlo". I don't know much about SMTP so I cannot explain the difference, and don't have time to research much. Perhaps someone with more knowledge can explain.

Then, type "auth login" and you will receive the following:

334 VXNlcm5hbWU6

This is essentially the word "Username" encoded in Base 64. Using a Base 64 encoder such as this one, encode your user name and enter it. Do the same for your password, which is requested next. You should see:

235 2.7.0 Accepted

And that's it, you're logged in.

There is one more oddity to overcome if you're using OSx or Linux terminals. Just pressing the "ENTER" key does not apparently result in a CRLF which SMTP needs to end a message. You have to use "CTRL+V+ENTER". So, this should look like the following:

^M
.^M
250 2.0.0 OK