Python POP3 Mail

Source: Internet
Author: User

#-*-Coding:utf-8-*-import poplibimport emailfrom email.parser import parserfrom email.header import Decode_headerfrom Email.utils import Parseaddr "to receive messages in Python's Poplib module in two steps: The first step is to use the POP3 protocol to get the message locally, the second step is to use the email module to parse the original message into a Message object, and then, Display the content of the message to the user in the appropriate form. "' #猜测编码格式def Guess_charset (msg): CharSet = Msg.get_charset () if CharSet is None:content_type = Msg.get (' Con Tent-type ', '). Lower () pos = Content_type.find (' charset= ') if pos >= 0:charset = Content_typ E[pos + 8:].strip () return charset# decode def decode_str (s): value, CharSet = Decode_header (s) [0] if charset:v Alue = Value.decode (charset) return value# parse message contents def print_info (msg, indent=0): if indent = = 0:for header in [' From ', ' to ', ' Subject ']: value = Msg.get (header, ') if Value:if header== ' Subject '                    : value = decode_str (value) else:hdr, addr = parseaddr (value) Name = 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)) Prin T_info (part, indent + 1) else:content_type = Msg.get_content_type () If content_type== ' text/plain ' or con            tent_type== ' text/html ': content = Msg.get_payload (decode=true) charset = Guess_charset (msg) If charset:content = Content.decode (charset) print ('%stext:%s '% (' * indent, content + ' ... ') else:print ('%sattachment:%s '% (' * indent, content_type)) email = raw_input (' email: ') PASSW Ord = Raw_input (' Password: ') pop3_server = raw_input (' POP3 server: ') #加密 #server = Poplib. Pop3_ssl (pop3_server, port = 995) Server = Poplib. POP3 (pop3_server) #server. Set_debuglevel (1) print (Server.getwelcome ()) # Authentication: server.user (email) server.pass_ (password) print ( ' Messages:%s. Size:%s '% Server.stat ()) RESP, mails, octets = Server.list () # Get the latest message, note that the index number starts at 1: RESP, lines, octets = SE RVER.RETR (len (mails)) # parse message: Msg = Parser (). PARSESTR (' \ r \ n '. Join (lines)) # Print message content: Print_info (msg) # Caution: Messages will be deleted directly from the server: # Server.dele (len (mails)) # Close connection: Server.quit ()

Reference Https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001408244819215430d726128bf4fa78afe2890bec57736000.

Python POP3 Mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.