1POP andImap-post Office Protocol andInternet Message Access Protocol2 3 the protocol used to download messages from the SMTP Server.4 5POP-The Post Office Protocol6 by Poplib linked servers,7 example,8 ImportSYS9 Importpoplib, emailTenHost ="' OneUserID ='userid' APW ='PW' -Storedir ="' #Email Stored directory (the mailbox) -P =Poplib. POP3 (host) the Try: - p.user (userid) - P.pass_ (PW) - exceptPoplib.error_proto as E: + Print("Login failed:", E) - sys.exit () + AMaillist = P.list () [1]#The list of message in the mailbox at Print("%d mails."%Len (maillist)) -Dellist = [] - - forIteminchMaillist:#Email Download -Number, octets = Item.split (' ') - Print("Start downloading mail%s (%s Bytes)"%(number, octets)) inLines = P.RETR (number) [1]#retrieve the ' number ' th email -msg = Email.message_from_string ("\ n". Join (lines))#Email Object to With Open (Storedir) as FH: +Fh.write (msg.as_string (unixfrom=1) +"\ n") - dellist.append (number) the Print("downloaded mail%s (%s Bytes)"%(number, octets)) * $Counter =0Panax Notoginseng forNuminchDellist:#Delete Email -Counter + = 1 the Print("Deleting Mail%d of%d"%(counter, Len (dellist))) +P.dele (number)#Delete Mail A the Print("%d emails were deleted from server"%counter) +P.quit ()#Logout from server - $IMAP-Internet Message Access Protocol $ IMAP is more sophisticated and more powerful than POP protocol -Example, opens a mailbox andRetrieves andPrints all messages: - the ImportGetpass, Imaplib -M =Imaplib. IMAP4 ()Wuyi M.login (Getpass.getuser (), Getpass.getpass ()) the M.select () -Typ, data = M.search (None,' All') Wu forNuminchdata[0].split (): -Typ, data = M.fetch (num,'(RFC822)') About Print('Message%s\n%s\n'% (num, data[0][1])) $ m.close () - m.logout () - - Reference, A python doc, +Https://docs.python.org/3/library/imaplib.html
POP and Imap-post Office Protocol and Internet Message Access Protocol