How to Solve SMTP Email Sending Issues?

佳龙 张 0 Reputation points
2025-03-07T09:39:26.13+00:00
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

def send_test_email():
    smtp_server = 'smtp-mail.outlook.com'
    smtp_port = 587  # 使用 TLS 的端口
    sender_email = 'my email'  # 替换为你的发件邮箱
    password = 'password'  # 替换为你的邮箱密码
    receiver_email = 'test@email'  # 替换为接收者邮箱
    
    # 创建邮件内容
    subject = "SMTP 测试邮件"
    body = "这是一封用于测试连接SMTP服务器的邮件,请忽略。"

    # 创建 MIMEMultipart 对象
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = receiver_email
    message['Subject'] = Header(subject, 'utf-8')
    
    # 附加邮件内容
    message.attach(MIMEText(body, 'plain', 'utf-8'))
    
    try:
        # 创建 SMTP 客户端并连接到服务器
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()  # 启动 TLS 加密
        server.login(sender_email, password)  # 登录邮箱
        server.sendmail(sender_email, receiver_email, message.as_string())  # 发送邮件
        print("测试邮件发送成功!")
    except smtplib.SMTPException as e:
        print(f"邮件发送失败: {e}")
    finally:
        server.quit()  # 退出SMTP连接

if __name__ == "__main__":
    send_test_email()


/bin/python3 demo.py

邮件发送失败: (535, b'5.7.3 Authentication unsuccessful [SG2PR02CA0105.apcprd02.prod.outlook.com 2025-03-07T07:28:44.694Z 08DD5BDCE053084B]')

Outlook | Windows | Classic Outlook for Windows | For business
{count} votes

1 answer

Sort by: Most helpful
  1. Joan Hua-MSFT 5,300 Reputation points Microsoft External Staff
    2025-03-10T02:53:03.58+00:00

    Hi @佳龙 张

    Welcome to our forum!

    What is your account? Outlook.com or something else?

    What account type are you configuring? (IMAP/POP/Echange?)

    As i know, Basic Authentication no longer available to access any Outlook account. If your Outlook is configured to connect to Outlook.com using POP or IMAP, Modern Authentication is not supported. This is a known issue.

    Instead of using POP/IMAP and SMTP, it is suggested please create a new Outlook Desktop profile and then add your Outlook.com account using automatic account configuration, which will add the account with Modern Authentication.  More information: Modern Authentication Methods now needed to continue syncing Outlook Email in non-Microsoft email apps - Microsoft Support 

    Please kindly understand that the Outlook tag here we mainly focus on general issues about Outlook desktop client. If you want to learn more about the code, please go to the forum with Office Development tag. In addition, here is the detailed settings used to verify IMAP/POP in the past, more: POP, IMAP, and SMTP settings for Outlook.com - Microsoft Support.User's image

    Hope it helps! 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  

    Note: Please follow the steps in our [documentation] to enable e-mail notifications if you want to receive the related email notification for this thread.  


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.