xsmtp.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # update: 2021-1-27-10
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from email.header import Header
  5. class Client(object):
  6. def __init__(self, username='zhangyouqian@xxx.com', password='DEVdev123',
  7. smtp_host='smtp.exmail.qq.com', smtp_port=465):
  8. self.username = username
  9. self.password = password
  10. self.smtp_host = smtp_host
  11. self.smtp_port = smtp_port
  12. def send_mails(self, addrs=['zhangyouqian@secdeer.com'],
  13. title='test-0404', text='test', addressee='尊敬的用户', addresser='xxx'):
  14. # addrs.append('daien@secdeer.com')
  15. # addrs.append('lijiaming@secdeer.com')
  16. message = MIMEText(text, 'plain', 'utf-8')
  17. message['Subject'] = Header(title, 'utf-8')
  18. message['From'] = Header(addresser, 'utf-8')
  19. message['To'] = Header(addressee, 'utf-8')
  20. try:
  21. server = smtplib.SMTP_SSL(host=self.smtp_host, port=self.smtp_port)
  22. server.login(self.username, self.password)
  23. server.sendmail(from_addr=self.username, to_addrs=addrs, msg=message.as_string())
  24. print('邮件发送成功')
  25. return True
  26. except Exception as e:
  27. import traceback
  28. print('Error: %s' % e.__class__.__name__)
  29. print('Message: %s' % str(traceback.format_exc()))
  30. print('邮件发送失败')
  31. return False
  32. if __name__ == '__main__':
  33. s = Client()
  34. s.send_mails(addrs=['xuzhongyiqiang@xxx.com'])