zoukankan      html  css  js  c++  java
  • 【selenium+Python unittest】之使用smtplib发送邮件错误:smtplib.SMTPDataError:(554)、smtplib.SMTPAuthenticationError(例:126邮箱)

    原代码如下:

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    #要发送的服务器
    smtpserver = 'smtp.126.com'
    #要发送的邮箱用户名/密码
    user = 'XXX@126.com'
    password = 'XXX'
    #发送的邮箱
    sender = 'XXX@126.com'
    #接收的邮箱
    receiver = 'XXX@qq.com'
    #发送邮箱主题
    subject = 'test_mail'
    
    #编写HTML类型的邮件正文
    msg = MIMEText('<html><h1>大佬好!</h1></html>','html','utf-8')
    msg['Subject'] = Header(subject,'utf-8')
    
    #连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(user,password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()

    一、现象:

    发送邮件时,运行时报错smtplib.SMTPDataError,如下图:

    二、解决办法

    ①经网上查询得知:因为126邮箱中没有开启【授权码】,如下图所示应该开启:

     ②但是再次运行代码还是报错:smtplib.SMTPAuthenticationError,如下图,提示登陆失败:

    原因是:代码中的密码应该改为授权密码即可。

    ③继续运行后,但是代码还是报错:smtplib.SMTPDataError:(554, b'DT:SPM 126 smtp4

    报错原因是没有加上下面的代码:

    #报错原因是因为“发件人和收件人参数没有进行定义
    msg['from'] = 'test_bug@126.com'
    msg['to'] = 'testyao@163.com'

    加上之后,终于解决发送邮件失败的问题了。

    完整代码如下:(因保密自行替换)

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    #要发送的服务器
    smtpserver = 'smtp.126.com'
    #要发送的邮箱用户名/密码
    user = 'XXX@126.com'
    password = 'XXX'
    #发送的邮箱
    sender = 'XXX@126.com'
    #接收的邮箱
    receiver = 'XXX@qq.com'
    #发送邮箱主题
    subject = 'test_mail'
    
    #编写HTML类型的邮件正文
    msg = MIMEText('<html><h1>大佬好!</h1></html>','html','utf-8')
    msg['Subject'] = Header(subject,'utf-8')
    msg['from'] = 'XXX@126.com'
    msg['to'] = 'XXX@qq.com'
    
    
    #连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(user,password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()
  • 相关阅读:
    Unity The Method Signature Matching Rule
    Unity The Property Matching Rule
    Unity The Type Matching Rule
    Unity The Custom Attribute Matching Rule
    Unity The Member Name Matching Rule
    Unity No Policies
    Unity The Return Type Matching Rule
    Unity The Parameter Type Matching Rule
    Unity The Namespace Matching Rule
    关于TSQL递归查询的(转)
  • 原文地址:https://www.cnblogs.com/Owen-ET/p/8409141.html
Copyright © 2011-2022 走看看