开发者社区> 问答> 正文

python中pop收邮件password是什么

python中pop收邮件password是什么

展开
收起
云计算小粉 2018-05-10 20:11:06 2304 0
1 条回答
写回答
取消 提交回答
  • Python 用POP接收邮件
    一、简介
      POP(Post Office Protocal)最长用的POP版本是POP3,因此本文是以POP3为主。POP3非常简单,可以用来从邮件服务器上下载邮件,然后删除这些邮件。功能非常有限,后面讲解的IMAP完胜它,不过作为入门级的,还是有必要介绍一下,也对学习SMTP有帮助。
      Python提供了poplib模块,它提供了使用POP的便利接口。
    二、实例
      由于pop3功能较IMAP非常有限,而且我最后的程序并没有使用pop3,所以,不详细讲解,下面通过一个例子来说明下较为常见的功能。
      这个例子的功能为进入邮箱,查看所有的邮件。首先显示邮件的发件人、主题,查看邮箱主题内容。

    1. 需要模块

    import email, poplib, sys

    1. 连接POP3服务器,登录个人邮箱账户
        poplib提供POP3()方法和POP3_SSL()方法连接POP3服务器,区别和SMTP一样。gmail仍然使用POP3_SSL()方式,并返回class POP3实例

    p = poplib.POP3_SSL('pop.gmail.com')
      使用POP3.user(), POP3.pass_()方法来登录个人账户

    try:

    p.user(user) 
    p.pass_(passwd)

    except poplib.error_proto: #可能出现的异常

    print('login failed')
    1. 现在已经进入个人账户,下一步,利用POP3.list()函数查看邮箱内邮件信息。

      关于list()函数的详细说明,请点击这里。
    list()函数有三个返回值,分别是:response, listings, octets

    response 应答信息,我测试中出现的结果:
        

      以b开头的字符串是Byte类型,我在实际测试的时候,返回的信息几乎都是Byte类型的。关于此类型及和普通字符串的转化会在后面举例说明。

    listings 是形如['message_id message_size',...]若干各message-id和message_size构成的list。后面就是通过message_id来检索邮件。我测试中出现的结果:
      

    octets 不是特别清楚啥意思。
    response, listings, octets = p.list()

    1. 最重要的就是listings数据

      如上面解释的,listings是个list类型的数据,接下来我们取出listings中的message_id,也就是上面的 "1" "2" "3" "4" ...

    for listing in listings: #每次需要一个listing
    number, size = listing.split() #由于number和size是以空格分隔,所以利用split()函数分开,split()默认以' '为分隔
      现在我们就取出了我们需要的message_id,也就是number,注意number需要从Byte类型转化为字符串类型。

    1. POP3.top()函数

      利用此函数,取出邮件的headers,如下:

    response, lines, octets = p.top(number , 0)
      lines存储内容,下面先转化成Message类型(lines默认为标准字符串类型,仅供说明,以实际代码为准)

    message = email.message_from_string('n'.join(lines))

    1. 已经生成Message类,可以利用头部信息来查看From, Subject等信息

    for header in 'From', 'To', 'Subject', 'Date':

    if header in message:
        print(header + ':' , message[header]) 

      注意,此时的message[header]可能不会输出我们想看到的内容,有可能出现格式错乱问题,比如中英文的转化,所以还需要特殊来处理。处理方式请继续往下看IMAP部分。

    1. 取出邮件所有信息
        上面的top()函数只取出header信息以及根据参数确定的n行内容,如果用户希望查看邮件所有内容,那利用POP3.retr()函数取出

    response, lines, octets = p.retr(number)
      还是将lines中的内容转换成Message类型:

    message = email.message_from_string('n'.join(lines))

    1. 已经有了邮件所有信息,可以通过Message.get_payload()取出邮件正文了。

      但是,get_payload()函数并不一定返回邮件正文。以下是官方说明:
    Return the current payload, which will be a list of Message objects when is_multipart() is True, or a string when is_multipart() is False.
      在实际测试中,返回的就是a list of Message objects,这个问题困扰我很长时间,最终还是解决了,通过以下方法:

    复制代码
    maintype = message.get_content_maintype()
    if maintype == 'multipart':

    for part in message.get_payload():
        if part.get_content_maintype() == 'text':
            mail_content = part.get_payload(decode=True).strip()

    elif maintype == 'text':

    mail_content = e.get_payload(decode=True).strip()        

    复制代码

    1. 此时,mail_content就是邮件正文了.

      当然,如果是中文的话,这件事仍未完,还需要将它转化未'gbk',利用如下方式:

    mail_content = mail_content.decode('gbk')

    1. 到现在,基本已经大功告成了,能够取出邮箱中所有的邮件,并查看邮件的header内容和邮件正文了^_^
      三、完整代码:

    复制代码

    -- encoding:utf-8 --

    -- encoding:gbk --

    import email, getpass, poplib, sys

    hostname = 'pop.gmail.com'
    user = 'myUserName@gmail.com'
    passwd = '*'

    p = poplib.POP3_SSL('pop.gmail.com') #与SMTP一样,登录gmail需要使用POP3_SSL() 方法,返回class POP3实例
    try:

    # 使用POP3.user(), POP3.pass_()方法来登录个人账户
    p.user(user) 
    p.pass_(passwd)

    except poplib.error_proto: #可能出现的异常

    print('login failed')

    else:

    response, listings, octets = p.list()
    for listing in listings:
        number, size = listing.split() #取出message-id
        number = bytes.decode(number) 
        size = bytes.decode(size) 
        print('Message', number, '( size is ', size, 'bytes)')
            print()
        response, lines, octets = p.top(number , 0)
        # 继续把Byte类型转化成普通字符串
        for i in range(0, len(lines)):
            lines[i] = bytes.decode(lines[i])
        #利用email库函数转化成Message类型邮件
        message = email.message_from_string('\n'.join(lines))
        # 输出From, To, Subject, Date头部及其信息
        for header in 'From', 'To', 'Subject', 'Date':
            if header in message:
            print(header + ':' , message[header]) 
        #与用户交互是否想查看邮件内容
        print('Read this message [ny]')
        answer = input()
        if answer.lower().startswith('y'):
            response, lines, octets = p.retr(number) #检索message并返回
            for i in range(0, len(lines)):
                lines[i] = bytes.decode(lines[i])
            message = email.message_from_string('\n'.join(lines)) 
            print('-' * 72)
            maintype = message.get_content_maintype()
            if maintype == 'multipart':
                for part in message.get_payload():
                    if part.get_content_maintype() == 'text':
                mail_content = part.get_payload(decode=True).strip()
            elif maintype == 'text':
                mail_content = e.get_payload(decode=True).strip()
            try:
                mail_content = mail_content.decode('gbk')
            except UnicodeDecodeError:
                print('Decoding to gbk error')
                sys.exit(1)
            print(mail_content)
        print()
        print('Delete this message? [ny]')
        answer = input()
        if answer.lower().startswith('y'):
            p.dele(number)
            print('Deleted')

    finally:

    print('log out')
    p.quit()
        
    2019-07-17 22:25:33
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载