python发送邮件 示例

示例1

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.header import Header
 4 def sedmail():
 5     sender = 'liruixin@hfax.com'
 6     receivers = ['363572453@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
 7 
 8     # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
 9     message = MIMEText('Python 邮件发送测试...内容', 'plain', 'utf-8')
10     message['From'] = Header("李瑞鑫", 'utf-8')
11     message['To'] =  Header("363572453", 'utf-8')
12 
13     subject = 'Python SMTP 标题'
14     message['Subject'] = Header(subject, 'utf-8')
15 
16 
17     try:
18         smtpObj = smtplib.SMTP('localhost')
19         smtpObj.sendmail(sender, receivers, message.as_string())
20         print ("邮件发送成功")
21     except smtplib.SMTPException:
22         print ("Error: 无法发送邮件")
23 
24 for i in range(10):
25     sedmail()
原文地址:https://www.cnblogs.com/liruixin/p/6344312.html