Python发送邮件

 python smtplib发送邮件

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

# 发送邮件
sender = 'xxx@xxx.com'  # 发送邮件的人
receivers = ['xxx@xxx.com']  # 接收邮件人

# 第三方SMTP服务
mailHost = 'smtp.qq.com'  # 设置发送服务器
mailUser = 'xxx@xxx.com'  # 登录邮箱名
mailPass = 'xxxxxxxxxxxxx'  # 口令(授权码)

# 三个参数:第一个是文本内容,第二个plain设置文本格式,第三个utf-8设置编码
message = MIMEText(html, 'html', 'utf-8')
message['From'] = 'xxx@xxx.com'  # 发送者
message['To'] = 'xxx@xxx.cn'  # 接收者
subject = 'python发送邮件'  # 发送邮件标题
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP_SSL(mailHost, 465)  # 发送服务器的端口号
    smtpObj.connect(mailHost, 465)
    smtpObj.ehlo()  # 若不加这行,在服务器环境会报错SMTPServerDisconnected("Connection unexpectedly closed")
    smtpObj.login(mailUser, mailPass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print('邮件发送成功')
except smtplib.SMTPException:
    print('邮件发送失败')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值