首页 > 其他分享 >smtplib.SMTPException: No suitable authentication method found

smtplib.SMTPException: No suitable authentication method found

时间:2022-10-11 12:35:45浏览次数:40  
标签:code No resp AUTH self authentication SMTPException smtplib method


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:
<strong> raise SMTPException("No suitable authentication method found.")
</strong>

抛出异常的地方是上面代码中加粗的地方,主要是当前连接支持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)

标签:code,No,resp,AUTH,self,authentication,SMTPException,smtplib,method
From: https://blog.51cto.com/u_14754853/5746091

相关文章

  • HUAWEI nova 9 SE等7款设备开启HarmonyOS 3 Beta版尝鲜招募!
     HarmonyOS3新一轮升级进展来了!本次共有HUAWEInova9SEHUAWEIMatePad10.8英寸等 7款华为手机、平板开启HarmonyOS3Beta版尝鲜招募。感兴趣的小伙伴快来......
  • .gitignore 无法工作
    在开发一个新项目时,发现每次编译时都会产生一些.obj无用的文件,这些文件并不需要push到github上故使用.gitignore 忽略这些文件首先,我们可以设置这些文件的输出目......
  • 对象转JSONObject——字段空值不显示处理办法
    <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.71</version></dependency>importcom.alibaba.fastjson.J......
  • Nodejs安装
    1.安装node.js2.设置淘宝镜像npmconfigsetregistryhttps://registry.npm.taobao.org/ ,检查查看:npmconfiggetregistry3.配置npm下载插件的默认安装目录和缓......
  • HUAWEI nova 9 SE等7款设备开启HarmonyOS 3 Beta版尝鲜招募!
    HUAWEInova9SE等7款设备开启HarmonyOS3Beta版尝鲜招募!​HarmonyOS3新一轮升级进展来了!本次共有HUAWEInova9SEHUAWEIMatePad10.8英寸等 7款华为手机、平板开启H......
  • Solution Set -「NOIP Simu.」20221011
    「Unknown」找  给出平面上\(n\)个点,对于每个点,求出它到其他点的欧式距离平方和.  \(n\le2\times10^5\).  Tag:「水题无tag」  画风正常的签到题.\(d......
  • BUUCTF [NewStarCTF] Week1 WEB NotPHP 详解
    NotPHP<?phperror_reporting(0);highlight_file(__FILE__);if(file_get_contents($_GET['data'])=="WelcometoCTF"){if(md5($_GET['key1'])===md5($_GET['k......
  • Innodb-缓冲池
    缓冲池缓存这个东西是在开发当中使用特别多的东西,理解他也特别重要虽然我们现在工业开发都是使用的第三方的缓存如redis,但是Mysql的缓存也是比较重要的东西。如果出现......
  • Stack map does not match the one at exception handler
    Stackmapdoesnotmatchtheoneatexceptionhandler   ......
  • [51nod 1393] 0和1相等串 前缀和
    #include<cstdio>#include<cstring>#definemaxn1000050#definemax(a,b)((a)>(b)?(a):(b))usingnamespacestd;intl[maxn<<1];//即maxn*2inta[maxn];chars......