我正在尝试使用 Python 和 aiosmtpd 库在端口 465 上设置 SMTPS 服务器。虽然我可以使用 openssl s_client 在本地连接到服务器,但外部邮件服务器(如 Gmail)无法连接,并报告“连接被拒绝”错误。
这是我当前的代码:
import asyncio
import ssl
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Debugging
async def main():
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(
certfile='./certs/fullchain.pem',
keyfile='./certs/privkey.pem'
)
controller = Controller(
handler=Debugging(),
hostname='0.0.0.0',
port=465,
ssl_context=ssl_context
)
controller.start()
print("SMTPS server started on port 465...")
try:
await asyncio.sleep(3600)
finally:
controller.stop()
if __name__ == "__main__":
asyncio.run(main())
但是,当我禁用 TLS 加密并将端口号更改为 25 时,它可以正常工作。
我已经验证:
- 服务器启动时没有出现错误。
- 我可以使用 连接到我的服务器
openssl s_client -connect my_domain.example.com:465
。 - 我的防火墙中端口 465 已打开。
- SSL 证书有效且与域名匹配。
我知道端口 587 上也有带有 STARTTLS 协议的 SMTP,但在我的特定用例中,我需要端口 465 上的 SMTPS。
附加信息:
Python 版本:3.12.3。操作系统:Ubuntu 24 服务器
任何帮助或指导都将不胜感激。谢谢