使用 Microsoft 365 邮件 ID 发送邮件时出现 SMTP 身份验证错误

SMTP Authentication error while sending mail using microsoft 365 mail id

提问人:userr 提问时间:11/13/2023 更新时间:11/13/2023 访问量:38

问:

我想使用已启用 MFA 的 Microsoft 365 邮件 ID 发送邮件...

这是我尝试过的代码

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Replace these with your actual values
sender_email = 'sendermail.com'
app_password = 'pwd'
recipient_email = 'recipientmai.com'

# Create a MIME message
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = recipient_email
message['Subject'] = 'Test Email'
body = 'This is a test email sent using smtplib.'
message.attach(MIMEText(body, 'plain'))

# Connect to the SMTP server
with smtplib.SMTP('smtp.office365.com', 587) as server:
    server.starttls()
    # Login with your email and app password
    server.login(sender_email, app_password)
    # Send the email
    server.sendmail(sender_email, recipient_email, message.as_string())

print('Email sent successfully!')

当我执行上面的操作时,我得到了以下错误

Authentication unsuccessful, user is locked by your organization's security defaults policy. Contact your administrator

请帮我修复此错误

Python 电子邮件

评论


答: 暂无答案