一、代码

# 输入邮件地址, 口令和POP3服务器地址:
import datetime
import email
import poplib
import email.policy
from email.parser import Parser
flag=False

emailaddress = '******'
# 注意使用开通POP,SMTP等的授权码
password = '*******'
pop3_server = '*******'

# 连接到POP3服务器:
server = poplib.POP3(pop3_server)
# 可以打开或关闭调试信息:
# server.set_debuglevel(1)
# POP3服务器的欢迎文字:
print(server.getwelcome())

# 身份认证:
server.user(emailaddress)
server.pass_(password)

# stat()返回邮件数量和占用空间:
messagesCount, messagesSize = server.stat()
print('messagesCount:', messagesCount)
print('messagesSize:', messagesSize)
# list()返回所有邮件的编号:
resp, mails, octets = server.list()
print("resp:", resp)
print("mails:", mails)
print("octets:", octets)

length = len(mails)

print(length)

for i in range(length):
    i=length-i
    resp, lines, octets = server.retr(i)
    msg_content=b'\r\n'.join(lines)
    msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content)
    print('发件人', msg['from'])
    print('收件人', msg['to'])
    print('主题', msg['subject'])
    print('第一个收件人用户名', msg['to'].addresses[0].username)
    print('第一个发件人用户名', msg['from'].addresses[0].username)
    theme= msg['subject']
    email_content=[part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0]
    if theme=="启动查库任务分派机器人" and email_content==datetime.datetime.now().strftime("%Y%m%d"):
        flag=True
        break

server.quit()
print(flag)
12-20 15:59