# update: 2021-1-27-10 import smtplib from email.mime.text import MIMEText from email.header import Header class Client(object): def __init__(self, username='zhangyouqian@xxx.com', password='DEVdev123', smtp_host='smtp.exmail.qq.com', smtp_port=465): self.username = username self.password = password self.smtp_host = smtp_host self.smtp_port = smtp_port def send_mails(self, addrs=['zhangyouqian@secdeer.com'], title='test-0404', text='test', addressee='尊敬的用户', addresser='xxx'): # addrs.append('daien@secdeer.com') # addrs.append('lijiaming@secdeer.com') message = MIMEText(text, 'plain', 'utf-8') message['Subject'] = Header(title, 'utf-8') message['From'] = Header(addresser, 'utf-8') message['To'] = Header(addressee, 'utf-8') try: server = smtplib.SMTP_SSL(host=self.smtp_host, port=self.smtp_port) server.login(self.username, self.password) server.sendmail(from_addr=self.username, to_addrs=addrs, msg=message.as_string()) print('邮件发送成功') return True except Exception as e: import traceback print('Error: %s' % e.__class__.__name__) print('Message: %s' % str(traceback.format_exc())) print('邮件发送失败') return False if __name__ == '__main__': s = Client() s.send_mails(addrs=['xuzhongyiqiang@xxx.com'])