Using SMTP, Gmail, and STARTTLS

本文探讨了如何通过Telnet与Gmail的SMTP服务器进行邮件发送操作。介绍了正确的端口设置(587用于STARTTLS和465用于SMTP over SSL),并解释了为何直接使用HELO命令无法成功连接。还提供了使用OpenSSL工具来实现TLS协商的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

So I'm learning about SMTP and am trying to use telnet to send some mail over SMTP.

I've easilly been able to send mail to my gmail account via:

$ host gmail.com
...
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com.
...
$ telnet gmail-smtp-in.l.google.com 25
Trying 74.125.142.27...
...
Connected to gmail-smtp-in.l.google.com.
...
HELO <me@test.com>
...

However, I'm having trouble sending from my gmail account. From what I understand about SMTP, I should be using SMTP to send mail from < mygmailaddress@gmail.com > to the outgoing gmail SMTP servers, which in turn use SMTP to transfer the mail to the receivers incoming SMTP server ect.

However, I'm having difficulties. If I telnet into smtp.gmail.com via port 465 (gmail outgoing smtp mail server canonical), I'm immediately disconnected after starting with HELO <blah@blah.com>, or asked to STARTTLS. I can't find answers on how to proceed.

Any help is appreciated.

Sidenote: Currently I'm using Starbucks free Wi-Fi to access the internet. I'm actually unable to telnet directly from my computer (No route to host error). Instead, it only works if I ssh into a remote linux box on my school's network first, then telnet from there. Any idea why this is?

Thanks!



First of all, it looks like you're using the wrong port. Gmail exposes port 465 for SMTP over SSL and port 587 for SMTP with STARTTLS, as documented here. The difference between these two is that SMTP over SSL first establishes a secure SSL/TLS connection and conducts SMTP over that connection, and SMTP with STARTTLS starts with unencrypted SMTP and then switches to SSL/TLS. This is why you don't get a response to your HELO.

$ telnet smtp.gmail.com 587
Trying 74.125.25.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP fr1sm24834956pbb.26 - gsmtp
HELO <me@test.com>
250 mx.google.com at your service
STARTTLS
220 2.0.0 Ready to start TLS

But even if you telnet to port 587 you still aren't going to be able to send any email by hand. In order to do anything interesting you will have to STARTTLS, and you won't be able to handle the SSL/TLS binary protocol to negotiate the encryption.



The telnet client will not negotiate a TLS session for you. You should use another tool, such as OpenSSL's s_client. The following issues the STARTTLS command for you and handles the TLS negotiation:

$ openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf

Alternatively, you could connect directly to the SMTPS port:

$ openssl s_client -connect smtp.gmail.com:465 -crlf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值