Python教程

Python邮件发送

本文主要是介绍Python邮件发送,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 1 # coding = utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 from email.header import Header
 5 class SendMail:
 6     def __init__(self, mail_host):
 7         self.mail_host = mail_host
 8     def send(self, title, content, sender, auth_code, receivers):
 9         message = MIMEText(content, 'html', 'utf-8')
10         message['From'] = "{}".format(sender)
11         message['To'] = ",".join(receivers)
12         message["Subject"] = title
13         try:
14             smtp_obj = smtplib.SMTP_SSL(self.mail_host, 465)  # 启用ssl发信,端口一般是465
15             smtp_obj.login(sender, auth_code)  # 登录
16             smtp_obj.sendmail(sender, receivers, message.as_string())
17             print("Mail 发送成功")
18         except Exception as e:
19             print(e)
20 
21 if __name__ == '__main__':
22     mail = SendMail("smtp.126.com")
23     sender = "beiwei@126.com"
24     receivers = ['211@qq.com','985@qq.com']
25     title = "你眉目如当年,流转我心间"
26     content = """
27     car.net
28     <a href="https://car.net">车联网</a>
29     """
30     # 授权码不是邮箱登录密码,网易邮箱可以通过 "设置"->客户端授权秘密,自己注册和用自己的授权码,这个会删除
31     auth_code = "HFGBAP"
32     mail.send(title, content, sender, auth_code, receivers)

 

这篇关于Python邮件发送的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!