大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
SMTP:邮件发送协议
SMTP(Simple Mail Transfer Protocol)负责 从发件人到收件人邮件服务器 的投递。运维场景包括:监控告警邮件、应用事务通知(注册验证、密码重置)、批量营销(需合规)。本课衔接 networking-essentials 端口知识,MX 记录见 dns-essentials,服务器操作见 linux-essentials,配置管理见 git-essentials。
学习目标
- 说出 SMTP 常用端口 25 / 587 / 465 的区别与使用场景
- 用
swaks或telnet手工测试 SMTP 会话 - 理解 MTA → MTA 中继 与应用发信(Submission)的差异
SMTP 端口对照
| 端口 | 名称 | 用途 | 加密 |
|---|---|---|---|
| 25 | SMTP | 服务器间 MTA 中继 | 通常明文,部分 STARTTLS |
| 587 | Submission | 客户端/应用 提交 邮件 | STARTTLS 或 TLS |
| 465 | SMTPS | 隐式 TLS(legacy) | 全程 SSL/TLS |
应用发信(推荐路径)
App ──587 STARTTLS──► Postfix/relay ──25──► 收件方 MX
25 端口 常被云厂商封禁(防 spam);应用应走 587 + 认证。
手工测试 SMTP
# 安装 swaks(SMTP 测试瑞士军刀)
# Debian/Ubuntu: sudo apt install swaks
# 通过 Submission 端口发测试信
swaks --to ops@example.com \
--from noreply@example.com \
--server smtp.example.com:587 \
--auth LOGIN \
--auth-user noreply@example.com \
--auth-password 'secret' \
--tls
# openssl 查看 STARTTLS 握手
openssl s_client -connect smtp.example.com:587 -starttls smtp
# 然后输入:
# EHLO localhost
# AUTH LOGIN
# (base64 用户名/密码)
# MAIL FROM:<noreply@example.com>
# RCPT TO:<ops@example.com>
# DATA
# Subject: test
#
# body
# .
# QUIT
Postfix 最小发信配置(示意)
/etc/postfix/main.cf:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_security_level = encrypt
# /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 apikey:SG.xxxxx
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd*
sudo systemctl reload postfix
生产更常见 relay 到 SendGrid / AWS SES / 阿里云邮件推送,而非自建对外 MTA。
应用层集成
Spring Boot application.yml:
spring:
mail:
host: smtp.example.com
port: 587
username: noreply@example.com
password: ${MAIL_PASSWORD}
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
Node.js nodemailer 同理指定 port: 587, secure: false(STARTTLS)。
排障 checklist
- DNS:MX 记录是否正确?(dns-essentials)
- 端口:
nc -zv smtp.example.com 587 - TLS:
openssl s_client -connect ... -starttls smtp - 认证:用户名密码 / API Key 是否正确
- 日志:
journalctl -u postfix -f或 relay 服务商控制台
练习
- 用
swaks向自己的邮箱发一封测试信,记录完整 SMTP 对话 - 解释 25 与 587 端口在 DevOps 场景下各自的角色
- 查看 Spring Boot 或你常用框架的 mail 配置,确认使用 587 + STARTTLS
小结
SMTP = 邮件投递协议;应用发信走 587 Submission + TLS + 认证,服务器间中继走 25。 下一课讲 IMAP——客户端如何收取与同步邮件。