使用Python smtplib发送邮件(无需应用密码)遇[Errno 11003]错误
问题解决:smtplib发送Outlook邮件出现socket.gaierror错误
错误原因
你遇到的socket.gaierror: [Errno 11003] getaddrinfo failed是因为SMTP服务器地址填错了。smtplib.SMTP()的第一个参数需要填写邮件服务商的SMTP服务器域名,而非你的邮箱地址。Outlook/Office365的官方SMTP服务器是smtp.office365.com,不是你写的blabla@outlook.com。
修正后的代码
替换正确的服务器地址,同时优化邮件格式的规范性:
import smtplib from email.message import EmailMessage # 基础配置 sender = "blabla@outlook.com" receiver = "albalb@outlook.com" password = "你的邮箱登录密码" # 构建标准邮件内容 msg = EmailMessage() msg["Subject"] = "Tester" msg["From"] = sender msg["To"] = receiver msg.set_content("Hello") # 发送流程 with smtplib.SMTP("smtp.office365.com", 587) as prt: prt.starttls() prt.login(sender, password) prt.send_message(msg)
不使用应用密码的注意事项
- 先确认Outlook账户已开启POP/IMAP服务:登录Outlook网页版,进入「设置→邮件→同步电子邮件」,检查POP和IMAP状态为启用。
- 若账户未开启双重验证,需在微软账户安全中心开启「允许低安全性应用访问」(部分区域或账户默认关闭该选项)。
- 双重验证开启状态下,普通密码无法用于SMTP登录,必须使用应用密码——这是微软的强制规则,无法绕过。
- 若仍无法连接,排查本地防火墙是否拦截了SMTP端口587。
内容的提问来源于stack exchange,提问作者user21282037
相关产品推荐
相关产品推荐

