Connecting to smtp.gmail.com via command line
Asked Answered
I

10

51

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?

Isar answered 4/10, 2009 at 16:4 Comment(1)
S
66

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
Seldan answered 7/12, 2012 at 23:53 Comment(7)
For osx at least, you can add "-crlf" to the initial 'openssl' connection to make it work nicely with RETURNS. e.g. openssl s_client -connect smtp.gmail.com:465 -crlfRemains
That's almost two years since you posted this but indeed that's a great answer. Thanks!Complex
Still works like a charm. If only stupid Gmail didn't ask me to login using my web browsers from my server where I only have a console ;/.Micrometer
Does anyone happen to know if/how this would work if your Google account has two-factor authentication turned on?Wargo
"Type in HELO and you'll receive..." No you won't. You'll get a "HELO command without arguments is not allowed" error, at least in 2017. What you should type is "HELO smtp.gmail.com".Elysha
If you can't login even with the right password, see the various security measures that need to be flagged here #32760Impediment
To answer the MFA question, following this article allowed me to login from CLI: support.google.com/accounts/answer/…Laughingstock
A
8

For OSX' terminal:

openssl s_client -connect smtp.gmail.com:25 -starttls smtp 
Amoy answered 26/8, 2010 at 4:59 Comment(0)
L
8

Start session from terminal:

openssl s_client -connect smtp.gmail.com:25 -starttls smtp

The last line of the response should be "250 SMTPUTF8"

Initiate login

auth login

This should return "334 VXNlcm5hbWU6".

Type username

Type your username in base64 encoding (eg. echo -n 'your-username' | base64)

This should return "334 UGFzc3dvcmQ6"

Type password

Type your password in base64 encoding (eg. echo -n 'your-password' | base64)

Success

You should see "235 2.7.0 Accepted" and you're are successfully logged in

Lux answered 15/5, 2018 at 12:25 Comment(2)
For base64 encode use this echo -n 'email or pass' | base64Equate
Thanks @SerhiiPolishchuk. Updated the answer.Lux
G
6

Gmail require SMTP communication with their server to be encrypted. Although you're opening up a connection to Gmail's server on port 465, unfortunately you won't be able to communicate with it in plaintext as Gmail require you to use STARTTLS/SSL encryption for the connection.

Genniegennifer answered 4/10, 2009 at 16:8 Comment(0)
K
4

Jadaaih, you can connect send SMTP through CURL - link to Curl Developer Community.

This is Curl Email Client source.

Kinsfolk answered 4/10, 2009 at 16:20 Comment(0)
M
3

Try this:

telnet smtp.gmail.com 587
Muriel answered 8/11, 2009 at 13:5 Comment(4)
Then what? What do you do?Hertz
It's stated in the question that the port 587 wasn't working and that he already tried what you're providing here. You're bringing nothing of value in this thread.Henning
Downvoted because unhelpful and no explanation of what to do afterwards.Arletha
Maybe he was thinking if this returns an error it could be taken to mean that the port is blocked on your end.Greenwich
J
2

Check this post in lifehacker : Geek to Live: Back up Gmail with fetchmail . It uses a command line program. Check and see if it helps. BTW why are you using command line when there are many other nice alternatives?

Jean answered 4/10, 2009 at 17:36 Comment(1)
Hey Shoban, I am just trying to find or write an action script that does the mail sending to use it in my flex applicationIsar
S
2

tcp/465 was initially intended for establishing the SSL(and newer TLS) layer first, and inside doing cleartext or plain old protocols (smtp here)

tcp/587 was intended as a replacement to default tcp/25 port initially when spammers and mass mailing attacks commenced like a decade or more ago, but also during those infamous AOL ages, when some funny ISP had some blocks on default ports outbound (such as that tcp/25) for denying their own customers (AOL) to mass-send emails/spam back then, but AOL-customers needing to use alternative mail-accounts and mail-providers still needed to send their mails from AOL-internet connections, so they could still connect to tcp/587 and do simple smtp on it back then.

The deal with the STARTTLS way to do smtp is to use the two well known originally plain-text tcp/25 and tcp/587 ports, and only when the initial clear-text connect suceeded, to then START the TLS layer (thus STARTTLS) from there on, having a secured connection from that point onwards.

As for debugging these kind of things maybe via command-line tools, for example for windows there is the historical blat command line mailer (smtp), which up till today cant do TLS (STARTTLS) so it can only use plain-text smtp to send its mails.

http://www.blat.net/

Then there are numerous projects freeware and open source software that have more capabilities and features, such as

smtp client: mailsend @ googlecode http://code.google.com/p/mailsend/

smtp client: msmtp @ sourceforge (related to mpop below) http://msmtp.sourceforge.net/

pop3 client: mpop @ sourceforge http://mpop.sourceforge.net/

Stucker answered 9/1, 2014 at 14:51 Comment(0)
M
2

How to connect to "Google SMTP mail server" from the command line?

1] SSL connect command

openssl s_client -connect {{server_name}}:{{server_port}} -crlf -quiet -starttls smtp

with variables

  • server_name: smtp.gmail.com
  • server_port: 587
  • user_name__hash: echo -n '{{user_name}}' | base64
  • user_password__hash: echo -n '{{user_password}}' | base64

2] SMTP mail server commands - every command in one line

auth login
{{user_name__hash}}
{{user_password__hash}}
helo {{server_name}}
mail from: <{{message_from}}>
rcpt to: <{{message_to}}>
DATA
from: <{{message_from}}>
to: <{{message_to}}>
subject:{{message_subject}}
Content-Type: text/html; charset='UTF-8'; Content-Transfer-Encoding: base64;
MIME-Version: 1.0
{{message_content}}
.
quit
Missi answered 30/10, 2019 at 18:4 Comment(0)
E
0

gmail uses an encrypted connection. So, even after you establish a connection, you wont be able to send any email. The encryption is a little complex to manage. Try using openssl instead.

The thread below should help-

How to send email using simple SMTP commands via Gmail?

Epictetus answered 22/7, 2015 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.