#coding: utf-8
import smtplib
from email.mime.text import mimetext
from email.header import header
sender = '×××@163.com'
receiver = '×××@qq.com'
subject = 'python email test2'
smtpserver = 'smtp.163.com'
username = '×××'
password = '×××' # 授权码,不是密码
msg = mimetext('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
msg['subject'] = header(subject, 'utf-8')
msg["to"]=receiver
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
------------如果passwd不是授权码的话,报错如下-------------------
jack@jack-desktop:~/work/script/test$ python testemai.py
traceback (most recent call last):
file "testemai.py", line 19, in
smtp.login(username, password)
file "/home/jack/anaconda/lib/python2.7/smtplib.py", line 622, in login
raise smtpauthenticationerror(code, resp)
smtplib.smtpauthenticationerror: (535, 'error: authentication failed')
jack@jack-desktop:~/work/script/test$ python testemai.py
traceback (most recent call last):
file "testemai.py", line 19, in
smtp.login(username, password)
file "/home/jack/anaconda/lib/python2.7/smtplib.py", line 622, in login
raise smtpauthenticationerror(code, resp)
smtplib.smtpauthenticationerror: (535, 'error: authentication failed')
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号