# -*- coding: utf-8 -*-
import poplib
from email import parser
host = 'pop.gmail.com'
username = 'mine@gmail.com'
password = '*******'
pop_conn = poplib.POP3_SSL(host)
pop_conn.user(username)
pop_conn.pass_(password)
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print message['Subject']
pop_conn.quit()
优点: 可以输出内容
缺点: 只检测一次 2. 使用第三方插件 chilkat
# -*- coding: utf-8 -*-
import sys
import chilkat
host = 'pop.gmail.com'
username = 'mine@gmail.com'
password = '******'
# The mailman object is used for receiving (POP3)
# and sending (SMTP) email.
mailman = chilkat.CkMailMan()
# Any string argument automatically begins the 30-day trial.
success = mailman.UnlockComponent("30-day trial")
if (success != True):
print "Component unlock failed"
sys.exit()
# Set the GMail account POP3 properties.
mailman.put_MailHost(host)
mailman.put_PopUsername(username)
mailman.put_PopPassword(password)
mailman.put_PopSsl(True)
mailman.put_MailPort(995)
# Read mail headers and one line of the body.
# To get the full emails, call CopyMail instead (no arguments)
bundle = mailman.GetAllHeaders(1)
if (bundle == None ):
print mailman.lastErrorText()
sys.exit()
for i in range(0,bundle.get_MessageCount()):
email = bundle.GetEmail(i)
# Display the From email address and the subject.
print email.ck_from()
print email.subject() + "\n"