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()