发邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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  =  'BuGaoSuNi@163.com'
         toaddrs  =  [ '136xxxx1475@139.com' 'xxxx@xx.com' ]
         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 ])
 
         =  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()



收邮件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#encoding=utf-8
import  poplib
import  email
 
if  __name__  = =  '__main__' :
         =  poplib.POP3( 'pop.163.com' )
         M.user( 'BuGaoSuNi@163.com' )  
         M.pass_( 'BuGaoSuNi' )
    
         #打印有多少封信  
         numMessages  =  len (M. list ()[ 1 ])  
         print  'num of messages' , numMessages  
 
    
         #从最老的邮件开始遍历
         for  in  range (numMessages):
                 =  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' ))






      本文转自Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1919745,如需转载请自行联系原作者