邮件
- 使用python调用第三方邮箱群发邮件第三方邮件需要设置授权码这里是163邮箱,图上传不了。设置 === >邮箱安全设置 === >客户端授权密码
- 代码部分:
- # ! /usr/bin / env python#__ * __coding: utf - 8__ * __
- import smtplib from email.mime.text import MIMEText as mimetext from email.mime.multipart import MIMEMultipart
- #发件服务器地址mail_host = 'smtp.163.com'#发件邮箱地址sender_user = [email protected]#mail_pass = 'xxxxx'#登录密码#邮箱授权码,不是登录密码sender_pass = 'xxxx'#收件邮箱地址receivers = [[email protected], [email protected]]
- #邮件内容message = mimetext('Python 邮件测试发送', 'plain', 'utf - 8')#发送邮箱地址message['From'] = sender_user#群发邮件时会报错message['To']不支持列表,使用join函数把地址合成字符串message['To'] = ",".join(receivers)#邮件主题subject = 'Python SMTP 邮件测试'message['Subject'] = subject#
- try: # smtpobj = smtplib.SMTP()# smtpobj.connect(mail_host, 25)# smtpobj.login(mail_user, mail_pass)# smtpobj.sendmail(sender, receivers, message)# print '邮件发送成功'# except: # print "邮件发送失败"smtpobj = smtplib.SMTP() smtpobj.connect(mail_host, 25) smtpobj.login(sender_user, sender_pass) smtpobj.sendmail(sender_user, receivers, message.as_string()) smtpobj.close()
来源: http://www.bubuko.com/infodetail-2227631.html