Python 3.x from_addr=email.utils.getaddresses([from_addr])[0][1],使用python发送电子邮件时出错
使用python发送电子邮件时出现以下错误,是否需要创建对象Python 3.x from_addr=email.utils.getaddresses([from_addr])[0][1],使用python发送电子邮件时出错,python-3.x,email,smtplib,Python 3.x,Email,Smtplib,使用python发送电子邮件时出现以下错误,是否需要创建对象 import smtplib from email.message import EmailMessage email = EmailMessage() email['from']: 'xxxxx@gmail.com' email['to']: "xxxxx@gmail.com" email['subject']: 'Hey Buddy Python' email.set_content("Learning python") w
import smtplib
from email.message import EmailMessage
email = EmailMessage()
email['from']: 'xxxxx@gmail.com'
email['to']: "xxxxx@gmail.com"
email['subject']: 'Hey Buddy Python'
email.set_content("Learning python")
with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login("xxxx@gmail.com", "xxxx")
smtp.send_message(email)
程序的输出:
Traceback (most recent call last):
File "C:/Users/xxxxx/Desktop/PYTHON/PyCharm/Email/SendEmail.py", line 15, in <module>
smtp.send_message(email)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 940, in send_message
from_addr = email.utils.getaddresses([from_addr])[0][1]
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38-32\lib\email\utils.py", line 112, in getaddresses
all = COMMASPACE.join(fieldvalues)
TypeError: sequence item 0: expected str instance, NoneType found
回溯(最近一次呼叫最后一次):
文件“C:/Users/xxxxx/Desktop/PYTHON/PyCharm/Email/sendmail.py”,第15行,在
smtp.send_邮件(电子邮件)
文件“C:\Users\xxxxx\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py”,第940行,在发送消息中
from_addr=email.utils.getaddresses([from_addr])[0][1]
文件“C:\Users\xxxxx\AppData\Local\Programs\Python\Python38-32\lib\email\utils.py”,第112行,在getaddresses中
all=COMMASPACE.join(字段值)
TypeError:序列项0:应为str实例,未找到非类型
退出代码1的过程已完成您得到的错误是
NoneType
而不是string
,原因是您没有将值分配给电子邮件字典,如中所示
更具体地说,这里:
email['from']: 'xxxxx@gmail.com'
email['to']: "xxxxx@gmail.com"
email['subject']: 'Hey Buddy Python'
您应该使用赋值运算符,=
,而不是:
尝试:
email['from'] = 'xxxxx@gmail.com'
email['to'] = "xxxxx@gmail.com"
email['subject'] ='Hey Buddy Python'