smtplib

  • 廖雪峰的教程写的挺好的:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000
  • http://www.runoob.com/python/python-email.html
  • http://blog.csdn.net/marksinoberg/article/details/51506308
#!/usr/bin/env python                                                                                                                         
# -*- coding: utf-8 -*-

import smtplib,datetime,time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header

class Email(object):
    def __init__(self,email,project):
        self.email=email
        self.project=project

    def sendEmail(self):
        stmphost='127.0.0.1'
        stmpport=25
        smtp=smtplib.SMTP(smtphost,smtpport)
        username='shuffle'
        password='123'
        smtp.login(username,password)

        msg=MIMEMultipart('alternative')
        msg['uname']=username
        msg['password']=password

        if len(self.email) > 1:
            msg['To']=','.join(self.email)
        else:
            msg['To']=self.email[0]
        msg['From']='[email protected]'
        cc=[u'[email protected]']
        msg['Cc']=','.join(cc)

        html="""
        %s
        """ % (self.project,)
        part=MIMEText(html,'html','utf-8')
        msg.attach(part)

        subject='hello world!'
        msg['subject']=Header(subject,'utf-8')

        smtp.sendmail(msg['From'],self.email+cc,msg.as_string())
        smtp.quit()

if __name__=='__main__':
    try:
        email=Email('[email protected]',u'你好')
        email.sendEmail()
    except Exception,e:
        print e                  

你可能感兴趣的:(smtplib)