计网-命令行实现收发邮件

网易邮箱开启smtp/pop3服务

不建议使用QQ邮箱,目前已经不支持无加密的连接(测试日期2024.11.24)

直接在设置里,如下图,开启即可。开启需要短信验证。

开启之后会获得授权码。

开启telnet服务

在控制面板的程序->开启或关闭Windows功能中找到Telnet客户端,勾选。

命令行实现收发邮件

1.命令行中键入如下命令,连接QQ邮箱的发送邮件服务器。

telnet smtp.163.com 25

输入之后进入如下状态

2.输入如下命令,用以验证是否联通和表明身份(网易邮箱必须输入)

helo smtp

如同表示正常连通。

3.登录邮箱
输入 auth login,出现 334 加字符串,此时输入完整邮箱地址的编码,按下回车,再输入授权码的 64 位编码,按下回车,出现登陆成功的提示。
base64编码网站 : Base64 编码/解码 - 锤子在线工具 (toolhelper.cn)

auth login

#base64 encode过的账号

#base64 encode过的授权码

  1. 发送邮件

注意: 一定要顺序无误输入!!!

①输入发件人邮箱

mail from:<xxx@163.com>

②输入收件人邮箱

rcpt to:<xxx@163.com>

③输入data进入内容(包括主题)的编写

data

subject:Test20230611

注意,此刻要回车两次,空一行。
输入邮件正文

HelloWorld
.

以空一行和一个点( . ) 结束正文编辑。

④退出

quit
总结

open smtp.163.com 25

helo 163.com

auth login

邮箱base64编码

(授权码的base64编码)

mail from:<xxx@163.com>

rcpt to:<xxx@163.com>

data

Subject:Test2022

\n

\n

hello wolrd!

.

因为我发现如果你多次输错命令,或者一段时间没有交互,连接会断开,又要从头开始,烦人的很,于是就写了一个脚本。

自动化脚本

复制代码
import telnetlib
import base64
import time

# 设置SMTP服务器和端口
host = 'smtp.163.com'
port = 25

# 创建Telnet连接
tn = telnetlib.Telnet(host, port)
# print("Sending command:",'telnet smtp.163.com')
# 读取服务器欢迎信息
welcome_message = tn.read_until(b"\n")
print('Server Response:'+welcome_message.decode('utf-8'))

# 发送 HELO 命令
tn.write(b"helo smtp\r\n")
print("Sending command:"+'helo smtp')
response = tn.read_until(b"\n")
print('HELO Response:'+response.decode('utf-8'))

# 发送 AUTH LOGIN 命令
tn.write(b"auth login\r\n")
# print("Sending command:"+'auth login')
response = tn.read_until(b"\n")
print('AUTH LOGIN Response:'+response.decode('utf-8'))

# 输入用户名(Base64 编码)
username = ''  # 填入你的用户名
encoded_username = base64.b64encode(username.encode()).decode('utf-8')
tn.write(encoded_username.encode() + b"\r\n")
response = tn.read_until(b"\n")
print('Username Response:'+response.decode('utf-8'))

# 输入密码(Base64 编码)
password = ''  # 填入你的密码
encoded_password = base64.b64encode(password.encode()).decode('utf-8')
tn.write(encoded_password.encode() + b"\r\n")
response = tn.read_until(b"\n")
print('Password Response:'+response.decode('utf-8'))


### 发送邮件
#邮件内容
sender = ''  # 发件人邮箱
receiver = ''  # 收件人邮箱
subject = 'Test Email'  # 邮件主题
body = 'Hello, this is a test email sent via Python.'

# 命令行发送

# 发送 MAIL FROM 命令
mail_from_command = f"mail from:<{sender}>\r\n".encode('utf-8')
tn.write(mail_from_command)
response = tn.read_until(b"\n")
print("MAIL FROM Response: ", response.decode('utf-8'))

# 发送 RCPT TO 命令
rcpt_to_command = f"RCPT TO:<{receiver}>\r\n".encode('utf-8')
tn.write(rcpt_to_command)
response = tn.read_until(b"\n")
print("RCPT TO Response: ", response.decode('utf-8'))

# 发送 DATA 命令
tn.write(b"data\r\n")
response = tn.read_until(b"\n")
print("DATA Response: ", response.decode('utf-8'))
tn.write(b'Subject:'+subject.encode())

# 发送邮件正文内容
email_body = f"Subject: {subject}\r\n\r\n{body}\r\n.\r\n".encode('utf-8')
tn.write(email_body)
response = tn.read_until(b"\n")
print("Email Body Response: ", response.decode('utf-8'))

# 发送 QUIT 命令
tn.write(b"QUIT\r\n")
response = tn.read_until(b"\n")
print("QUIT Response: ", response.decode('utf-8'))

# 关闭连接
tn.close()
相关推荐
于冬恋39 分钟前
RabbitMQ高级
服务器·网络·rabbitmq
算力魔方AIPC2 小时前
如何用算力魔方4060安装PaddleOCR MCP 服务器
运维·服务器
tan77º2 小时前
【Linux网络编程】分布式Json-RPC框架 - 项目设计
linux·服务器·网络·分布式·网络协议·rpc·json
Ray Song2 小时前
【Linux】 wget、curl 用法区别
linux·运维·服务器·curl·wget
.Shu.3 小时前
计算机网络 各版本TLS握手的详细过程
网络·计算机网络
半路_出家ren3 小时前
路由综合实验RIP,OSPF,BGP
网络·网络协议·rip·ospf·ebgp·ibgp
小妖6663 小时前
本地文件夹即时变身 Web 服务器(文件服务器)
运维·服务器
张太行_4 小时前
网络SSL/TLS协议详解
网络·web安全·ssl
zhysunny5 小时前
Day22: Python涡轮增压计划:用C扩展榨干最后一丝性能!
c语言·网络·python
hhzz5 小时前
重温 K8s 基础概念知识系列八( K8S 高级网络)
网络·容器·kubernetes