Python發送Email方法執行個體_python

來源:互聯網
上載者:User

本文以執行個體形式展示了Python發送Email功能的實現方法,有不錯的實用價值的技巧,且功能較為完善。具體實現方法如下:

主要功能代碼如下:

#/usr/bin/env python# -*- encoding=utf-8 -*-import base64import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextclass CCSendMail:  def __init__(self,host="your.mailhost.com",username='fromuser@xxx.com',password='passwd'):    self.__smtp=smtplib.SMTP(host)    self.__subject=None    self.__content=None    self.__from=None    self.__to=[]    self.__style='html'    self.__charset='gb2312'    self.username = username    self.password = password    self.fromAlias='fromuser' #寄件者別名    self.from2=''      def close(self):    try:      self.__smtp.quit()    except Exception ,e:      pass    def setFromAlias(self,alias):    self.fromAlias=alias  def setStyle(self,style):    self.__style = style  def setFrom2(self,from2):    self.from2=from2      def setSubject(self,subject):    self.__subject=subject      def setContent(self,content):    self.__content=content      def setFrom(self,address):    self.__from=address      def addTo(self,address):    self.__to.append(address)      def setCharset(self,charset):    self.__charset=charset      def send(self):    try:      self.__smtp.set_debuglevel(1)            #login if necessary      if self.username and self.password:        self.__smtp.login(self.username,self.password)              msgRoot = MIMEMultipart('related')      msgRoot['Subject'] = self.__subject      aliasB6=base64.encodestring(self.fromAlias.encode(self.__charset))      if len(self.from2)==0:        msgRoot['From'] = "=?%s?B?%s?=%s"%(self.__charset,aliasB6.strip(),self.__from)      else:        msgRoot['From'] = "%s"%(self.from2)      msgRoot['To'] = ";".join(self.__to)            msgAlternative = MIMEMultipart('alternative')      msgRoot.attach(msgAlternative)            msgText = MIMEText(self.__content, self.__style,self.__charset)      msgAlternative.attach(msgText)      self.__smtp.sendmail(self.__from,self.__to,msgRoot.as_string())      return True    except Exception,e:      print "Error ",e      return False      def clearRecipient(self):    self.__to = []    #給單個人發送郵件  def sendHtml(self,address,title,content):    self.setStyle('html')    self.setFrom("<%s>"%self.username)    if not isinstance(content,str):      content = content.encode('gb18030')    self.addTo(address)    self.setSubject(title)    self.setContent(content)    ret = self.send()    self.close()    return ret    #群發郵件  def sendMoreHtml(self,addressList,title,content):    self.setStyle('html')    self.setFrom("<%s>"%self.username)    if not isinstance(content,str):      content = content.encode('gb18030')    for address in addressList:      self.addTo(address)    self.setSubject(title)    self.setContent(content)    ret = self.send()    self.close()    return ret#測試def main():  send=CCSendMail()  send.sendHtml('touser@xxx.com',u'郵件標題',u'郵件內容')  #send.sendMoreHtml([touser1@xx.com,touser2@xx.com],u'郵件標題',u'郵件內容') if __name__=='__main__':  main()

希望本文所述執行個體對大家的Python程式設計有一定的協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.