使用Python通过Outlook发送邮件时偶发SMTPDataError问题求助
使用Python通过Outlook发送邮件时出现间歇性SMTPDataError错误
我用Python脚本通过Outlook发送邮件时,偶尔会触发以下错误,但有时候邮件又能正常发送:
Traceback (most recent call last): File "c:\Users\Variable\Documents\python_files\learning_new\sending_email.py", line 23, in <module> session.sendmail(sender_address, receiver_address, text) File "C:\Users\Variable\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 908, in sendmail raise SMTPDataError(code, resp) smtplib.SMTPDataError: (554, b'5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message [BeginDiagnosticData]WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade.[EndDiagnosticData] [Hostname=PAXP193MB1710.EURP193.PROD.OUTLOOK.COM]')
我的Python代码如下:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText mail_content = '''Hello, This is a simple mail. There is only text, no attachments are there The mail is sent using Python SMTP library. Thank You''' # 邮件地址和密码 sender_address = 'Fahe_em@outlook.com' sender_pass = '*********' receiver_address = 'fah909190@gmail.com' # 配置MIME message = MIMEMultipart() message['From'] = sender_address message['To'] = receiver_address message['Subject'] = 'A test mail sent by Python. It has an attachment.' # 邮件主题 # 添加邮件正文 message.attach(MIMEText(mail_content, 'plain')) # 创建SMTP会话 session = smtplib.SMTP('smtp.office365.com', 587) session.starttls() # 启用安全连接 session.login(sender_address, sender_pass) # 登录账号 text = message.as_string() session.sendmail(sender_address, receiver_address, text) session.quit() print('Mail Sent')
问题原因分析
这个错误是Outlook反垃圾邮件机制触发的:
OutboundSpamException说明邮件被判定为疑似垃圾邮件,触发了临时发送限制- 间歇性出现是因为反垃圾系统的判定并非固定规则,会受发送频率、邮件内容一致性、接收方反馈等因素影响
解决办法
修正邮件内容矛盾点
- 你的邮件主题写了"It has an attachment",但正文明确说没有附件,这种矛盾描述会直接触发垃圾邮件判定,把主题改成贴合实际的内容,比如
"Python测试邮件:纯文本无附件" - 清理正文中冗余或易触发判定的表述,保持内容逻辑一致
- 你的邮件主题写了"It has an attachment",但正文明确说没有附件,这种矛盾描述会直接触发垃圾邮件判定,把主题改成贴合实际的内容,比如
控制发送频率与内容多样性
- 不要短时间内重复发送完全相同的邮件,可以在正文末尾添加随机内容(比如当前时间戳)
- 测试发送时,两次发送间隔至少1-2分钟,避免被判定为批量垃圾发送
检查Outlook账号状态
- 登录Outlook网页版,查看账号是否有发送限制提示,完成账号安全验证(比如二次验证)
- 确认账号没有被标记为可疑发送者
改用Microsoft Graph API发送
SMTP方式容易触发反垃圾机制,官方Graph API的发送逻辑更贴合Outlook的规则,稳定性更高,示例代码框架如下:import requests # 需先通过Azure应用注册获取access token access_token = "你的access token" endpoint = "https://graph.microsoft.com/v1.0/me/sendMail" email_data = { "message": { "subject": "Python测试邮件", "body": { "contentType": "Text", "content": "这是通过Microsoft Graph API发送的测试邮件" }, "toRecipients": [ { "emailAddress": { "address": "fah909190@gmail.com" } } ] }, "saveToSentItems": "true" } response = requests.post( endpoint, headers={"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}, json=email_data ) if response.status_code == 202: print("邮件发送成功") else: print(f"发送失败: {response.text}")
内容的提问来源于stack exchange,提问作者Faheem
相关产品推荐
相关产品推荐

