sendmai.py

import sys
import smtplib  
from email.mime.text import MIMEText  

class MessageAgent:
    def __init__(self):
        self.mail_host="partner.outlook.cn"
        self.mail_user="hello@qq.com"
        self.mail_pass="password"
        self.mail_from="hello@qq.com"

    def send_mail(self,subject,to_list,content):  
        me=self.mail_from
        msg=MIMEText(content,_subtype='html',_charset='utf-8')  
        msg['Subject'] = subject
        msg['From'] = me
        msg['To'] = ";".join(to_list)   
        try:  
            server = smtplib.SMTP()  
            server.connect(self.mail_host,587)
            server.ehlo()
            server.starttls()
            server.login(self.mail_user,self.mail_pass)  
            server.sendmail(me, to_list, msg.as_string())  
            server.close()  
            return True  
        except Exception, e:  
            print str(e)  
            return False  

if __name__ == '__main__':  
    if (sys.argv)>2:
        email_contact=[]
        subject=sys.argv[1]
        info=sys.argv[3]
        for seg in sys.argv[2].split(','):
            email=seg
            email_contact.append(email+"@qq.com")
        ma=MessageAgent()
        ma.send_mail(subject,email_contact,info)
原文地址:https://www.cnblogs.com/husbandmen/p/8626700.html