欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python之邮件发送简易篇

程序员文章站 2022-03-16 08:54:43
...
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "@qq.com"  # 用户名
mail_pass = ""  # 口令,QQ邮箱是输入授权码,在qq邮箱设置 里用验证过的手机发送短信获得,不含空格

sender = '@qq.com'
receivers = ['@qq.com', '@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('a test for python', 'plain', 'utf-8')
message['From'] = Header("ppyy", 'utf-8')
message['To'] = Header("you", 'utf-8')

subject = 'my test'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()
    print(u"邮件发送成功")

except smtplib.SMTPException as e:
    print (e)

注:此文部分;存在参考