python收发邮件(demo)--smtplib--poplib

发邮件

#encoding=utf-8

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

if __name__ == '__main__':
        fromaddr = '[email protected]'
        toaddrs = ['[email protected]', '[email protected]']
        subject = 'smtplib test.'

        content = 'smtplib test: hello, smtplib ! Can you see it ?'
        textApart = MIMEText(content)

        imageFile = '/tmp/code2.png'
        imageApart = MIMEImage(file(imageFile, 'rb').read(), imageFile.split('.')[-1])
        imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile.split('/')[-1])

        m = MIMEMultipart()
        m.attach(textApart)
        m.attach(imageApart)
        m['Subject'] = subject

        server = smtplib.SMTP('smtp.163.com')
        server.login(fromaddr,'BuGaoSuNi')
        server.sendmail(fromaddr, toaddrs, m.as_string())
        server.quit()



收邮件

#encoding=utf-8
import poplib
import email

if __name__ == '__main__':
        M = poplib.POP3('pop.163.com')
        M.user('[email protected]')  
        M.pass_('BuGaoSuNi')
   
        #打印有多少封信  
        numMessages = len(M.list()[1])  
        print 'num of messages', numMessages  

   
        #从最老的邮件开始遍历
        for i in range(numMessages):
                m = M.retr(i+1)
                msg = email.message_from_string('\n'.join(m[1]))
                #allHeaders = email.Header.decode_header(msg)
                aimHeaderStrs = {'from':'', 'to':'', 'subject':''}
                for aimKey in aimHeaderStrs.keys():
                        aimHeaderList = email.Header.decode_header(msg[aimKey])
                        for tmpTuple in aimHeaderList:
                                if tmpTuple[1] == None:
                                        aimHeaderStrs[aimKey] += tmpTuple[0]
                                else:
                                        aimHeaderStrs[aimKey] += tmpTuple[0].decode(tmpTuple[1]) #转成unicode
                for aimKey in aimHeaderStrs.keys():
                        print aimKey,':',aimHeaderStrs[aimKey].encode('utf-8') #转成utf-8显示

                for part in msg.walk(): #遍历所有payload
                        contenttype = part.get_content_type()
                        filename = part.get_filename()
                        if filename: #and contenttype=='application/octet-stream':
                                #保存附件
                                data = part.get_payload(decode=True)
                                file("mail%d.attach.%s" % (i+1,filename),'wb').write(data)
                        elif contenttype == 'text/plain':
                                #保存正文
                                data = part.get_payload(decode=True)
                                charset = part.get_content_charset('ios-8859-1')
                                file('mail%d.txt' % (i+1), 'w').write(data.decode(charset).encode('utf-8'))




你可能感兴趣的:(python,String,File,header,import)