Code Example:
from email.parser import Parser from email.header import decode_header from email.utils import parseaddr import poplib # Enter the e-mail address, password and POP3 server address: # In Email = INPUT ( 'In Email:') # password = INPUT ( 'Password:') # pop3_server = INPUT ( 'POP3 Server:') # Sina-mail test passes, password login password # Email = "[email protected]" # password = "password" # pop3_server = "pop.sina.cn" # QQ mailbox by testing, using the authorization code, use the ssl # Email = "[email protected]" # password = "titeexrfwjutbhhg" # pop3_server = "pop.qq.com" class Email: def __init__(self,account,password,pop3_server): self.account = account self.password =password self.pop3_server = pop3_server def guess_charset(self,msg): charset = msg.get_charset() if charset is None: content_type = msg.get('Content-Type', '').lower() pos = content_type.find('charset=') if pos >= 0: charset = content_type[pos + 8:].strip() return charset def decode_str(self,s): value, charset = decode_header(s)[0] if charset: value = value.decode(charset) return value def print_info(self,msg, indent=0): if indent == 0: for header in ['From', 'To', 'Subject']: value = msg.get(header, '') if value: if header == 'Subject': value = self.decode_str(value) else: hdr, addr = parseaddr(value) name = self.decode_str(hdr) value = u'%s <%s>' % (name, addr) print('%s%s: %s' % (' ' * indent, header, value)) if (msg.is_multipart()): parts = msg.get_payload() for n, part in enumerate(parts): print('%spart %s' % (' ' * indent, n)) print('%s--------------------' % (' ' * indent)) self.print_info(part, indent + 1) else: content_type = msg.get_content_type() if content_type == 'text/plain' or content_type == 'text/html': content = msg.get_payload(decode=True) charset = self.guess_charset(msg) if charset: content = content.decode(charset) print('%sText: %s' % (' ' * indent, content + '...')) else: print('%sAttachment: %s' % (' ' * indent, content_type)) DEF main (Self): # connection to the POP3 server: # Server = poplib.POP3 (pop3_server) # QQ requires SSL Server = poplib.POP3_SSL (pop3_server) # open or close the debug information: server.set_debuglevel (. 1 ) # can option: POP3 server welcome text print: Print (server.getwelcome () decode (. ' UTF-8 ' )) # authentication: server.user (Email) server.pass_(password) # STAT () Returns the number of messages and space: Print ( ' the Messages: Size% S:. S% ' % server.stat ()) # List () returns the number of all messages: RESP, mails, octets = server.list () # can view a list of return similar to [B'1 82923 ', B'2 2184', ...] Print (mails) # get the latest message, pay attention to the beginning of the index number from 1: index = len (mails) RESP, lines, octets = server.retr (index) # Each row of lines stored in the original text messages, # can be obtained throughout the text of the original message: msg_content = B ' \ R & lt \ n- ' .join (lines) .decode ( ' UTF-. 8 ' ) # later parse the message: MSG = . Parser () parsestr (msg_content) self.print_info(msg) # You can delete messages according to the index number directly from the mail server: # server.dele (index) # close the connection: server.quit () if __name__ == '__main__': email = "[email protected]" password = "titeexrfwjutbhhg" pop3_server = "pop.qq.com" Email(email,password,pop3_server).main()
。。