python poplib从电子邮件中获取标头

时间:2017-12-08 11:01:52

标签: python email pop3 poplib

这是我的actula代码:

import poplib, sys, re, threading, time, Queue
from email import parser

    Mailbox = poplib.POP3(pop3, '110')
    Mailbox.user(username)
    Mailbox.pass_(password)
    numMessages = len(Mailbox.list()[1])
    for i in range(numMessages):
            fullemail = ''
            fullemail = '\n'.join([msg for msg in Mailbox.retr(i+1)[1]])
            msg = parser.Parser().parsestr(fullemail)
            for part in msg.walk():
                    print part.get_payload(decode=True)

打印part.get_payload(decode = True)打印电子邮件正文,如何打印电子邮件标题?

1 个答案:

答案 0 :(得分:0)

items() / email.message.Message对象的方法email.message.EmailMessage可能有用:

for t in msg.items():
    print(t)

t是一个像这样的元组(key,value).