qq邮箱或163邮箱发送给别的邮箱
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
receiver = '***@***.com'
sender = 'qq号@qq.com' # 163的就用 '***@163.com'
send_server = 'smtp.qq.com' # 163的就用 'smtp.163.com'
username = 'qq号' # 163没改用户名的就可以写 username = sender, 不然会报异常: smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
password = 'smtp授权码' # 注意不是用账号的密码
# qq授权码获取参考 : https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256,
# 163授权码获取参考: https://zhinan.sogou.com/guide/detail/?id=316512447829
subject = 'python email test' # 这个主题可能会被识别为垃圾邮件, 建议换成正式的主题, 我尝试163发时会报异常: smtplib.SMTPDataError: (554, b'DT:SPM 163
msg = MIMEText('hello world ~', 'text', 'utf-8') # 中文需第二个参数是 “plain”, 第三个参数是‘utf-8’, 不然也会报上一行代码介绍的异常
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP()
smtp.connect(send_server)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
其它觉得写得不错的介绍文章:
过程很详细的: https://blog.youkuaiyun.com/DearMorning/article/details/81069075
功能代码比较多的:
http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html