Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to smtp.gmail.com via command line

Tags:

smtp

gmail

telnet

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?

like image 496
Abhishek Avatar asked Oct 04 '09 16:10

Abhishek


People also ask

How do I connect to Gmail SMTP?

Set up the app or device with the Gmail SMTP serverOn your device or in the app, enter smtp.gmail.com as the server address. In the Port field, enter one of the following numbers: If you're using SSL, enter 465. If you're using TLS, enter 587.


1 Answers

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 
like image 160
the911s Avatar answered Sep 19 '22 19:09

the911s