465和587是两个常用的SMTP(Simple Mail Transfer Protocol,简单邮件传输协议)端口,用于发送电子邮件。以下是对这两个端口的详细解释及其相关概念:
SMTP端口:
465端口的优势:
587端口的优势:
类型:
应用场景:
问题1:无法通过465或587端口发送邮件
telnet smtp.example.com 465
或 telnet smtp.example.com 587
。问题2:邮件发送后经常被标记为垃圾邮件
import smtplib
from email.mime.text import MIMEText
# SMTP服务器配置
smtp_server = 'smtp.example.com'
port = 587 # 或者 465 如果使用SMTPS
username = 'your_username'
password = 'your_password'
# 邮件内容
msg = MIMEText('这是一封测试邮件')
msg['Subject'] = '测试邮件主题'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
# 连接SMTP服务器并发送邮件
try:
server = smtplib.SMTP(smtp_server, port)
server.starttls() # 启动TLS加密
server.login(username, password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
print('邮件发送成功')
except Exception as e:
print(f'邮件发送失败: {e}')
finally:
server.quit()
请根据实际情况替换上述代码中的SMTP服务器地址、端口、用户名和密码等信息。希望这些信息能帮助你更好地理解和运用这两个SMTP端口。
领取专属 10元无门槛券
手把手带您无忧上云