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 Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,666 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Joan Hua-MSFT 4,955 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.  


  2. 佳龙 张 0 Reputation points
    2025-03-14T07:03:15.7966667+00:00
    What I need to solve is to automatically send emails via SMTP. Has the old STMP method been abandoned? You said to use automatic accounts to configure Outlook.com accounts. This will use modern authentication to add accounts. What is the specific operation method? I can't open the link behind
    
    0 comments No comments

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.