# -*- conding:utf-8 -*- from email.mime.text import MIMEText from email.utils import parseaddr, formataddr from email.header import Header import smtplib class SentMail: def __init__(self, sender, receiver, password): self.sender = sender self.receiver = receiver self.password = password def _format_addr(self, s): name, addr = parseaddr(s) print(name,addr) #formataddr里面是个元组 return formataddr((Header(name, 'utf-8').encode(), addr)) def sent(self, serverName, msg): message = MIMEText(msg, 'plain', 'utf-8') message['From'] = self._format_addr('哈哈 <%s>' % self.sender) # message['To'] = self._format_addr('嘻嘻 <%s>' % self.receiver) message['To'] = ",".join(to_list) message['Subject'] = Header('这是一个主题', 'utf-8').encode() server = smtplib.SMTP(serverName, 25) #打印日志信息 server.set_debuglevel(1) server.login(self.sender, self.password) server.sendmail(self.sender, self.receiver, message.as_string()) server.quit()
使用smtplib发送邮件练习
最新推荐文章于 2024-02-29 15:37:59 发布