python--应用场景--邮件发送

一、简单邮件发送

参考文档:https://docs.python.org/3.5/library/smtplib.html

#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText

class SmtpClient(object):
	def __init__(self,server_host,email_me,email_password):
		self.server_host = server_host
		self.email_me = email_me
		self.email_password = email_password
		
	def send(self,email_subject,email_to,email_content):
		message = MIMEText(email_content)
		message['Subject'] = email_subject
		message['From'] = self.email_me
		server = smtplib.SMTP(self.server_host)
		server.login(self.email_me,self.email_password)
		server.sendmail(self.email_me,email_to,message.as_string())
		server.quit()
		return True

if __name__ == '__main__':
	server_host = "smtp.qq.com"
	email_me = "[email protected]"
	email_password = 'your passwrod'
	email_client = SmtpClient(server_host,email_me,email_password)
	
	email_subject = 'python smtp test'
	email_to = '[email protected]'
	email_content = 'test message'
	print(email_client.send(email_subject,email_to,email_content))

 

转载于:https://my.oschina.net/u/3323607/blog/2249957

你可能感兴趣的:(python--应用场景--邮件发送)