编程语言
首页 > 编程语言> > 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)

 

标签:__,code,sender,receivers,Python,发送,mail,message,邮件
来源: https://www.cnblogs.com/chengxiazuohua/p/15393899.html