1、使用python编写发送邮件的类库
代码如下:
import smtplib
from email.mime.text import MIMEText
class SendEmail(object):
def __init__(self,send_mail_server):
self.__smtp = smtplib.SMTP(send_mail_server, port=25)
self.__From=""
self.__To=""
self.__password=""
self.__subject=""
self.__context=""
self.__mail=None
def __del__(self):
try:
self.__smtp.quit()
except:
pass
@property
def From(self):
return self.__From
@From.setter
def From(self, From):
self.__From = From
@property
def To(self):
return self.__To
@To.setter
def To(self, To):
self.__To = To
@property
def password(self):
return self.__password
@password.setter
def password(self,pwd):
self.__password=pwd
@property
def context(self):
return self.__context
@context.setter
def context(self,ctx):
self.__context=ctx
@property
def subject(self):
return self.__subject
@subject.setter
def subject(self,subject):
self.__subject=subject
def send(self):
self.__mail=MIMEText(self.__context)
self.__mail["From"]=self.__From
self.__mail["To"]=self.__To
self.__mail["Subject"]=self.__subject
self.__smtp.login(self.__From,self.__password)
self.__smtp.sendmail(self.__From, self.__To, self.__mail.as_string())
2、使用上述封装类发送邮件
直接使用上述封装类即可
send_email=SendEmail # 初始化的时候需要填入发送邮件服务器地址,如163的发送邮件服务器地址是
send_email.From="hitredrose # 发送邮件的邮箱地址
send_email.To="redrose2200 # 接收人的邮件地址
send_email.password="LRRDUVG-xxx-YHEMLK" # 发送邮件的邮箱的授权码,这里不能使用密码,要去邮箱配置授权码
send_email.subject="测试邮件标题" # 邮件标题
send_email.context="测试邮件内容\n你好啊,哈哈哈\n邮件结束" # 邮件内容
send_email.send()