zoukankan      html  css  js  c++  java
  • python smtplib发送邮件遇到的认证问题

    python的smtplib模块主要是用来发送邮件的,使用起来比较方便。

    使用程序发送邮件只需要写以下几行代码就OK了:

    #!/usr/bin/env python
    import smtplib
    s = smtplib.SMTP(mail server, port)
    s.login(username, passwd)
    s.sendmail(fromaddr, toaddrs, msg)

    不过使用这种方法不一定总是可行,昨天用这种方式发送邮件的时候程序总是会抛异常:

      File "/usr/lib64/python2.7/smtplib.py", line 617, in login
        raise SMTPException("No suitable authentication method found.")
    smtplib.SMTPException: No suitable authentication method found.
    

    查看python smtplib.py代码

            if not self.has_extn("auth"):
                raise SMTPException("SMTP AUTH extension not supported by server.")
    
            # Authentication methods the server supports:
            authlist = self.esmtp_features["auth"].split()
    
            # List of authentication methods we support: from preferred to
            # less preferred methods. Except for the purpose of testing the weaker
            # ones, we prefer stronger methods like CRAM-MD5:
            preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
    
            # Determine the authentication method we'll use
            authmethod = None
            for method in preferred_auths:
                if method in authlist:
                    authmethod = method
                    break
    
            if authmethod == AUTH_CRAM_MD5:
                (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
                if code == 503:
                    # 503 == 'Error: already authenticated'
                    return (code, resp)
                (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
            elif authmethod == AUTH_PLAIN:
                (code, resp) = self.docmd("AUTH",
                    AUTH_PLAIN + " " + encode_plain(user, password))
            elif authmethod == AUTH_LOGIN:
                (code, resp) = self.docmd("AUTH",
                    "%s %s" % (AUTH_LOGIN, encode_base64(user, eol="")))
                if code != 334:
                    raise SMTPAuthenticationError(code, resp)
                (code, resp) = self.docmd(encode_base64(password, eol=""))
            elif authmethod is None:
                raise SMTPException("No suitable authentication method found.")
    

    抛出异常的地方是上面代码中加粗的地方,主要是当前连接支持server并不支持[AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]

    中的任何一种认证方式,导致程序运行出现问题。

    解决方案是:在初始化SMTP和login之间调用starttls()方法就可以了,完整的代码如下:

    #!/usr/bin/env python
    import smtplib
    s = smtplib.SMTP(mail server, port)
    s.starttls()
    s.login(username, passwd)
    s.sendmail(fromaddr, toaddrs, msg)
    
  • 相关阅读:
    windows下cmd清屏命令cls
    mac电脑复制粘贴使用command+c command+v
    Git从远程仓库里拉取一条本地不存在的分支方法
    react系列笔记1 用npx npm命令创建react app
    golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息
    golang学习笔记9 beego nginx 部署 nginx 反向代理 golang web
    golang学习笔记8 beego参数配置 打包linux命令
    获取某一天所在周的开始日期和结束日期
    某一日期所在月份的天数
    获取某一日期所在月份的第一天日期或最后一天日期
  • 原文地址:https://www.cnblogs.com/bitCoin/p/5390385.html
Copyright © 2011-2022 走看看