#-*- MailClassLibrary -*-
import smtplib
import os
import sys
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class MailClassLibrary:
def __init__(self, mailType):
self.mailSendType = mailType
sender = "xsmhero@aa.com"
receiverList = ["xsmhero@aa.com"]
subject = ""
smtpServer = "www.mail.com"
userName = "aa@aacom"
password = "paword"
''' =========================方法================================ '''
# 中文乱码
def setChineseGarbled(self, text):
type = sys.getfilesystemencoding()
text = text.decode('UTF-8').encode(type)
return text
''' =========================================================== '''
''' 文件形式的邮件 '''
def sendContent(self, subject, content):
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = self.sender
msg['To'] = ";".join(self.receiverList)
try:
sn = smtplib.SMTP()
sn.connect(self.smtpServer)
sn.login(self.userName,self.password)
if(len(self.receiverList) > 1):
sn.sendmail(self.sender,self.receiverList ,msg.as_string())
else:
sn.sendmail(self.sender,msg['To'] ,msg.as_string())
sn.close()
except Exception, e:
print str(e)
''' HTML形式的邮件 '''
def sendHtml(self, subject, html):
msg = MIMEText(html, 'html', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = self.sender
msg['To'] = ";".join(self.receiverList)
try:
sn = smtplib.SMTP()
sn.connect(self.smtpServer)
sn.login(self.userName,self.password)
if(len(self.receiverList) > 1):
sn.sendmail(self.sender,self.receiverList ,msg.as_string())
else:
sn.sendmail(self.sender,msg['To'] ,msg.as_string())
sn.close()
except Exception, e:
print str(e)
''' 带图片的HTML邮件 '''
def sendPicHtml(self, subject, html, picPath):
picPath = self.setChineseGarbled(picPath)
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Header(subject, 'utf-8')
msgRoot['From'] = self.sender
msgRoot['To'] = ";".join(self.receiverList)
msgText = MIMEText(html,'html','utf-8')
msgRoot.attach(msgText)
fp = open(picPath, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
try:
sn = smtplib.SMTP()
sn.connect(self.smtpServer)
sn.login(self.userName,self.password)
if(len(self.receiverList) > 1):
sn.sendmail(self.sender,self.receiverList ,msgRoot.as_string())
else:
sn.sendmail(self.sender,msgRoot['To'] ,msgRoot.as_string())
sn.close()
except Exception, e:
print str(e)
''' 基于SSL的邮件 '''
def sendSSLContent(self, subject, content):
msg = MIMEText(content, 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = self.sender
msg['To'] = ";".join(self.receiverList)
try:
sn = smtplib.SMTP()
sn.connect(self.smtpServer)
sn.ehlo()
sn.starttls()
sn.ehlo()
sn.set_debuglevel(1)
sn.login(self.userName,self.password)
if(len(self.receiverList) > 1):
sn.sendmail(self.sender,self.receiverList ,msg.as_string())
else:
sn.sendmail(self.sender,msg['To'] ,msg.as_string())
sn.close()
except Exception, e:
print str(e)
#p = MailClassLibrary('text')
#p.sendAttach('rar','swire.rar','C://Documents and Settings//xusm//桌面//swire.rar')