Python 邮件发送消息

# 代码

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:supery

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr


class BaseMessage(object):
    def send(self, subject, body, to, name):
        raise NotImplementedError('未实现send方法')

class Email(BaseMessage):
    def __init__(self):
        self.email='1xx@163.com'
        self.user = 'Supery'
        self.pwd = 'xxx'

    def send(self, subject, body, to, name):
        msg = MIMEText(body,'plain','utf-8')
        msg['Form'] = formataddr([self.user,self.email])
        msg['To'] = formataddr([name,to])
        msg['Subject'] = subject

        server = smtplib.SMTP('smtp.163.com',25)
        server.login(self.email,self.pwd)
        server.sendmail(self.email,[to,],msg.as_string())
        server.quit()

#  使用

Email('90xxx9@qq.com','liu','你别走了','xxx太多了')

 

posted @ 2017-12-28 16:16  Pythia丶陌乐  阅读(205)  评论(0编辑  收藏  举报