zoukankan      html  css  js  c++  java
  • Python发送邮件smtplib.SMTP各报错问题的解决方法

    经测试可用的发送邮件代码:

    import smtplib
    from email.mime.text import MIMEText
     
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com"  # SMTP服务器
    mail_user = "username"  # 用户名
    mail_pass = "passwd"  # 密码(这里的密码不是登录邮箱密码,而是授权码)
     
    sender = 'sender_mail@163.com'  # 发件人邮箱
    receivers = ['receive_mail@qq.com']  # 接收人邮箱
     
     
    content = 'Python Send Mail !'
    title = 'Python SMTP Mail Test'  # 邮件主题
    message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title
     
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        print("mail has been send successfully.")
    except smtplib.SMTPException as e:
        print(e)
    

      

    问题解决与注意点:

    1.报错:Error: A secure connection is requiered(such as ssl)

    解决:因为邮箱SSL验证的问题,因此把smtplib.SMTP()改成smtplib.SMTP_SSL(),端口号为465

    2.报错:535, b'Error: authentication failed'

    解决:可能是由于用户名不正确,因此代码中的用户名应该填写为邮箱地址@前面部分 ,或是在邮箱设置的帐户昵称,如下图昵称Morning和马赛克部分,都可作为用户名

    3.SMTP服务器可根据发送的邮箱做相应的选择,如代码中使用163邮箱则设为mail_host = "smtp.163.com"  

    可以改成"smtp.126.com"、"smtp.qq.com"等等

    4.代码中的密码mail_pass为授权码,并非邮箱密码,授权码用于登录第三方邮件客户端的专用密码

    QQ邮箱可通过设置→帐户→生成授权码;网易邮箱126/163可通过设置→客户端授权密码

    原文:https://blog.csdn.net/dearmorning/article/details/81069075
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    [水煮 ASP.NET Web API2 方法论](3-6)万能路由
    [水煮 ASP.NET Web API2 方法论](3-5)路由约束
    [水煮 ASP.NET Web API2 方法论](3-4)设置路由可选项
    [水煮 ASP.NET Web API2 方法论](3-3)路由默认值
    [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由
    [转载自简书] ASPNetCore上传大文件碰到的一些问题总结
    [转载] 关于web.config
    [转载] sessionState 4种mode模式
    [转载] web.config 配置详解
    文档注释标记
  • 原文地址:https://www.cnblogs.com/cloak/p/11185171.html
Copyright © 2011-2022 走看看