python自动发邮件

 1 from email.header import Header
 2 from email.mime.text import MIMEText
 3 from email.utils import parseaddr,formataddr
 4 import smtplib
 5 from email.mime.multipart import MIMEMultipart
 6 from email.mime.image import MIMEImage
 7 
 8 # 发送普通文字
 9 def _format_addr(s):
10     name,addr = parseaddr(s)
11     return formataddr((Header(name,'utf-8').encode(),addr))
12 
13 from_addr = '834424581@qq.com'
14 password = 'rlu********bfe'#邮箱授权码
15 to_addr = '3153324684@qq.com'
16 smtp_sever = 'smtp.qq.com'
17 msg = MIMEText('我是自动发的,哈哈哈','plain','utf-8')
18 msg['From']=_format_addr('福龙 <%s>' % from_addr)
19 msg['To']=_format_addr('小号 <%s>' % to_addr)
20 msg['Subject']=Header('每日汇报','utf-8').encode()
21 server = smtplib.SMTP_SSL(smtp_sever,465,timeout=10)
22 server.set_debuglevel(0)
23 server.login(from_addr,password)
24 server.sendmail(from_addr,[to_addr],msg.as_string())
25 server.quit()

 

posted @ 2017-04-19 13:44  Erick-LONG  阅读(225)  评论(0编辑  收藏  举报