Python3 使用IMAP接收邮件

import imaplib
import email
M = imaplib.IMAP4_SSL("imap.sina.com")
print(M)
try:
    try:
        M.login('test@sina.com', '123456')
        print('登录成功')
    except Exception as e:
        print('login error: %s' % e)
        M.close()
    else:
        M.select()

        typ, data = M.search(None, 'ALL')
        print(len(data))
        for num in data[0].split():
            try:
                typ, data1 = M.fetch(num, '(RFC822)')
                msg = email.message_from_string(data1[0][1].decode('utf-8'))
                print(msg)
                print(msg["Date"])
            except Exception as e:
                print('got msg error: %s' % e)
        M.close()
        M.logout()
except Exception as e:
    print('imap error: %s' % e)
    M.close()
原文地址:https://www.cnblogs.com/rnckty/p/15480607.html