python3.6发送邮件

目的:python3.6发送邮件
环境:python3.6、pycharm

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 发送邮箱
sender = 'yuan**@***.com'
# 接收邮箱
receivers = ['511***[email protected]','222***[email protected]']

# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('Python 邮件: 正文 . . . \n第二行\n第三行', 'plain', 'utf-8')
# 发送者昵称
message['From'] = Header("小袁发送", 'utf-8')
# 接受者昵称
message['To'] = Header("小袁接受", 'utf-8')

# 邮件标题
subject = 'Python 邮件标题'
message['Subject'] = Header(subject, 'utf-8')

try:
    #qq邮箱服务器地址
    smtpObj = smtplib.SMTP('smtp.exmail.qq.com')
    # 登陆:账号,密码
    smtpObj.login(sender, '123***456')
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

你可能感兴趣的:(python)