import time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
class SendMail:
def send_mail(self,data):
#获得当前日期
strdate = time.strftime('%Y%m%d',time.localtime())# 第三方 SMTP 服务
mail_host="smtp.exmail.qq.com" #设置服务器
mail_user="zhangsan@126.com" #邮箱的登陆用户名
mail_pass="123456" #密码
#发送人
sender = 'zhangsan@126.com'
#接收人
receivers = ['lisi@126.com']message = MIMEText(data,_subtype='html',_charset='utf-8') #解决乱码 html格式
#message = MIMEText(data, 'plain', 'utf-8')
message['From'] = sender
message['To'] = ','.join(receivers)
#标题
subject = strdate+'数据'message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user,mail_pass)
print("登陆成功")
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print(e)
if __name__ == '__main__':
sendmail = SendMail()
sendmail.send_mail("发送邮件")