<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python實例26[sendemail]

       

      一 發(fā)送簡單的純文本郵件 

      import sys
      import os.path
      import smtplib
      import email

      def sendTextMail(mailhost,sender,recipients,ccto = '',bccto = '',subject = '',message = '', messagefile = ''):

        
      try:
          mailport
      =smtplib.SMTP(mailhost)
        
      except:
          sys.stderr.write(
      'Unable to connect to SMTP host "' + mailhost \
                            
      + '"!\nYou can try again later\n')
          
      return 0
          
        
      if(os.path.exists(messagefile) and os.path.isfile(messagefile)):
          with open(messagefile,
      'r') as f:
            message 
      += f.read()
          
        fullmessage 
      = 'From: ' + ' <' + sender + '> \n' +\
                    
      'To: ' + recipients + '\n' +\
                    
      'CC: '+ ccto + '\n' +\
                    
      'Bcc: ' + bccto + '\n' +\
                    
      'Subject: ' + subject + '\n' +\
                    
      'Date: ' + email.Utils.formatdate() +'\n' +\
                    
      '\n' +\
                    message

        
      try:
          
      #mailport.set_debuglevel(1)
          allrecipients = recipients.split(',')
          
      if not ccto == '':
            allrecipients.extend(ccto.split(
      ',')) 
          
      if not bccto == '':
            allrecipients.extend(bccto.split(
      ',')) 
          
      #print allrecipients
          #allrecipients must be list type
          failed = mailport.sendmail(sender, allrecipients, fullmessage)
        
      except Exception as e:
          sys.stderr.write(repr(e))
          
      return 0
        
      finally:
          mailport.quit()
          
        
      return 1

       

       

       注意:

      recipients,ccto,bccto是接收者的郵件地址,多個地址間使用,分割;

      message = '', messagefile = ''表示要發(fā)送的email的內(nèi)容,為可選參數(shù),messagefile表示email的內(nèi)容來自一個文本文件;

      mailport.sendmail(sender, recipients, message)中,message中的from/to/bcc等只是用來email的顯示,真正的收件人必須通過recipients來傳入,recipients必須為收件人的email地址的數(shù)組,

       

      二 發(fā)送html的郵件

      import sys
      import os.path
      import smtplib
      import email
      from email import encoders
      import mimetypes
      from email.mime.base import MIMEBase
      from email.message import Message
      from email.mime.audio import MIMEAudio
      from email.mime.image import MIMEImage
      from email.mime.text import MIMEText
      from email.mime.multipart import MIMEMultipart

      def getMIMEObj(path):
          ctype, encoding 
      = mimetypes.guess_type(path)
          
      if ctype is None or encoding is not None:
              
      # No guess could be made, or the file is encoded (compressed), so
              # use a generic bag-of-bits type.
              ctype = 'application/octet-stream'
          maintype, subtype 
      = ctype.split('/'1)
          msg 
      = None
          
      #print maintype 
          #print subtype
          if maintype == 'text':
              fp 
      = open(path)
              
      # Note: we should handle calculating the charset
              msg = MIMEText(fp.read(), _subtype=subtype, _charset='utf-8')
              fp.close()
          
      elif maintype == 'image':
              fp 
      = open(path, 'rb')
              msg 
      = MIMEImage(fp.read(), _subtype=subtype)
              fp.close()
          
      elif maintype == 'audio':
              fp 
      = open(path, 'rb')
              msg 
      = MIMEAudio(fp.read(), _subtype=subtype)
              fp.close()
          
      else:
              fp 
      = open(path, 'rb')
              msg 
      = MIMEBase(maintype, subtype)
              msg.set_payload(fp.read())
              fp.close()
              
      # Encode the payload using Base64
              encoders.encode_base64(msg)
          
      # Set the filename parameter
          msg.add_header('Content-Disposition''attachment', filename=os.path.basename(path))
          
      return msg

        
      def sendHtmlMail(mailhost,subject,sender,recipients,ccto = '',bccto = '',htmldir = '', htmlfile = ''):
        
      try:
          mailport
      =smtplib.SMTP(mailhost)
        
      except:
          sys.stderr.write(
      'Unable to connect to SMTP host "' + mailhost \
                            
      + '"!\nYou can try again later\n')
          
      return 0
          
        msgroot 
      = MIMEMultipart('related')
        msgroot[
      'From'= sender
        msgroot[
      'To'= recipients
        msgroot[
      'Cc'= ccto
        msgroot[
      'Bcc'= bccto
        msgroot[
      'Subject'= subject

        msghtml 
      = MIMEMultipart('alternative')
        msgroot.attach(msghtml)  
        
      if os.path.exists(os.path.join(htmldir,htmlfile)):
          htmlf 
      = open(os.path.join(htmldir,htmlfile))
          htmlmessage 
      = MIMEText(htmlf.read(),'html','utf-8')
          htmlf.close()
          msghtml.attach(htmlmessage)
          
        
      for root, subdirs, files in os.walk(htmldir):
          
      for file in files:
            
      if file == htmlfile:
              
      continue
            
      else:
              msgroot.attach(getMIMEObj(os.path.join(root,file)))
        
        failed 
      = 0
        
      try:
          
      #mailport.set_debuglevel(1)
          allrecipients = recipients.split(',')
          
      if not ccto == '':
            allrecipients.extend(ccto.split(
      ',')) 
          
      if not bccto == '':
            allrecipients.extend(bccto.split(
      ',')) 
          
      #print allrecipients
          failed = mailport.sendmail(sender, allrecipients, msgroot.as_string())
          
      print failed
        
      except Exception as e:
          sys.stderr.write(repr(e))
        
      finally:
          mailport.quit()
          
        
      return failed

       

       

      注意:

      htmldir參數(shù)表示要發(fā)送的html文件所在的目錄,此目錄下僅包含html文件和html文件引用的相關(guān)文件,且所有引用的文件必須與html都在同一目錄下,不能有子目錄;

      htmlfile參數(shù)表示要顯示到email中的html文件;

       

      例如:

      有html文件如下:

      d:\html

      |--myhtmlemail.html

      |--mylinkedtext.txt

      |--myimage.gif

       

      且myhtmlemail.html的內(nèi)容為:

      <html>

      <body>
      <a href="mylinkedtext.txt">message1</a>
      <br/>
      <h1>This is HTML email sent by python</h1>
      <img src="myimage.gif" alt="my message image" />
      </body>
      </html>

       

      則收到的email如下:

       

       

      三 使用如下:

      mailhost = 'mail-relay.example.com'
      sender  = "AAA@example.com"
      recipients = "BBB@example.com,EEE@example.com"
      ccto = 'CCC@example.com'
      bccto = 'DDD@example.com'
      subject = "Testing1"
      message = "Testing!\nTesting!\n"
      messagefile = "mymessage.txt"
      htmldir = 'D:\\html'
      htmlfile = 'myhtmleamil.html'
               
      sendTextMail(mailhost,sender,recipients,ccto,bccto,subject,message,messagefile)
      sendHtmlMail(mailhost, subject,sender,recipients,htmldir=htmldir,htmlfile=htmlfile)

       

      四 發(fā)送帶有附件的email

      import sys
      import os.path
      import smtplib
      import email  
      import mimetypes

      from email import encoders
      from email.mime.base import MIMEBase
      from email.message import Message
      from email.mime.audio import MIMEAudio
      from email.mime.image import MIMEImage
      from email.mime.text import MIMEText
      from email.mime.multipart import MIMEMultipart

      def getMIMEObj(path):
          ctype, encoding 
      = mimetypes.guess_type(path)
          
      if ctype is None or encoding is not None:
              
      # No guess could be made, or the file is encoded (compressed), so
              # use a generic bag-of-bits type.
              ctype = 'application/octet-stream'
          maintype, subtype 
      = ctype.split('/'1)
          msg 
      = None
          
      #print maintype 
          #print subtype
          if maintype == 'text':
              fp 
      = open(path)
              
      # Note: we should handle calculating the charset
              msg = MIMEText(fp.read(), _subtype=subtype, _charset='utf-8')
              fp.close()
          
      elif maintype == 'image':
              fp 
      = open(path, 'rb')
              msg 
      = MIMEImage(fp.read(), _subtype=subtype)
              fp.close()
          
      elif maintype == 'audio':
              fp 
      = open(path, 'rb')
              msg 
      = MIMEAudio(fp.read(), _subtype=subtype)
              fp.close()
          
      else:
              fp 
      = open(path, 'rb')
              msg 
      = MIMEBase(maintype, subtype)
              msg.set_payload(fp.read())
              fp.close()
              
      # Encode the payload using Base64
              encoders.encode_base64(msg)
          
      # Set the filename parameter
          msg.add_header('Content-Disposition''attachment', filename=os.path.basename(path))
          
      return msg

        
      def sendMail(mailhost,subject,sender,recipients,ccto = '',bccto = '',message = '', payloads = ''):
        
      try:
          mailport
      =smtplib.SMTP(mailhost)
        
      except:
          sys.stderr.write(
      'Unable to connect to SMTP host "' + mailhost \
                            
      + '"!\nYou can try again later\n')
          
      return 0
          
        msgroot 
      = MIMEMultipart('mixed')
        msgroot[
      'From'= sender
        msgroot[
      'To'= recipients
        msgroot[
      'Cc'= ccto
        msgroot[
      'Bcc'= bccto
        msgroot[
      'Subject'= subject

        alternative 
      = MIMEMultipart('alternative')
        msgroot.attach(alternative)  
        mes 
      = MIMEText(message,'plain','utf-8')
        alternative.attach(mes)
          
        
      for file in payloads.split(','):
            
      if os.path.isfile(file) and os.path.exists(file):
              msgroot.attach(getMIMEObj(file))
            
      else:
              sys.stderr.write(
      "The file %s cannot be found" % file)
        
        
      try:
          
      #mailport.set_debuglevel(1)
          allrecipients = recipients.split(',')
          
      if not ccto == '':
            allrecipients.extend(ccto.split(
      ',')) 
          
      if not bccto == '':
            allrecipients.extend(bccto.split(
      ',')) 
          
      #print allrecipients
          failed = mailport.sendmail(sender, allrecipients, msgroot.as_string())
          
      if failed == None:
            sys.stderr.write(failed)
            
      return 0
        
      except Exception as e:
          sys.stderr.write(repr(e))
          
      return 0
        
      finally:
          mailport.quit()
          
        
      return 1


      mailhost 
      = 'mail-relay.company.com'
      sender  
      = "AAA@Company.com"
      recipients 
      = "BBB@company.com"
      ccto 
      = 'CCC@company.com'
      bccto 
      = 'DDD@company.com'
      subject 
      = "Testing1"
      message 
      = "Testing!\nTesting!\n"     

      sendMail(mailhost,subject,sender,recipients,message 
      = message,payloads = 'c:\\test\\test.ba,C:\\test\s7\\mytable.txt')

       

      參考:

      http://canofy.javaeye.com/blog/265600

      http://docs.python.org/library/email-examples.html

      http://www.rzrgm.cn/itech/archive/2011/02/18/1958114.html

       

      完!

      posted @ 2011-02-22 13:13  iTech  閱讀(2559)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 九九热这里只有精品在线| 亚洲性一交一乱一伦视频| 亚洲国产成人精品无色码| 99热精国产这里只有精品| 成av免费大片黄在线观看| 亚洲男人天堂2018| 欧美激欧美啪啪片| 国内精品无码一区二区三区 | 国产麻豆成人精品av| 中文字幕av中文字无码亚| 夜夜躁狠狠躁日日躁| 亚洲国产精品久久电影欧美| 岳普湖县| 久久亚洲精精品中文字幕| 依依成人精品视频在线观看| 免费无码一区无码东京热| 91人妻无码成人精品一区91| 无码丰满人妻熟妇区| 亚洲AV色香蕉一区二区蜜桃小说| 黔西| 中文字幕亚洲人妻一区| 亚洲欧美高清在线精品一区二区| 中文字幕乱码中文乱码毛片| 亚洲春色在线视频| 久久精品国产亚洲av成人| 国产精品久久久久aaaa| 中文字幕人妻不卡精品| 国产玖玖玖玖精品电影| 久久精品亚洲成在人线av麻豆| 亚洲香蕉网久久综合影视| 国产午夜伦伦午夜伦无码| 日夜啪啪一区二区三区| 亚洲性av网站| www国产成人免费观看视频| 国产精品午夜福利合集| 欧美黑人又粗又大久久久| 国产在线一区二区不卡| 精品久久人人做爽综合| 日本无遮挡真人祼交视频| 国产黄色一区二区三区四区| 国产精品高清中文字幕|