发送普通文本邮件.py

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

# 第三方 SMTP 服务
# 设置服务器
mail_host = "smtp.qq.com"

# 用户名
mail_user = "[email protected]"

#获取授权码
mail_pass="mpaocydzpzfjidge"

# 发件人账号
sender = '[email protected]'

# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
receivers = ['[email protected]']

send_content = 'Python 邮件发送测试...'

# 第一个参数为邮件内容,第二个设置文本格式,第三个设置编码
message = MIMEText(send_content, 'plain', 'utf-8')

# 发件人
message['From'] = Header("我是发件人", 'utf-8')

# 收件人
message['To'] = Header("我是收件人", 'utf-8')

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

try:
smtpObj = smtplib.SMTP()
# 25 为 SMTP 端口号
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")

猜你喜欢

转载自www.cnblogs.com/zhang-da/p/12231694.html