我编写了一个程序来使用 poplib 读取 Outlook 电子邮件,并将过滤后的数据仅输出到 json 中,然后删除所有电子邮件。但是使用我编写的代码并不是删除所有电子邮件而只删除一封电子邮件。有谁能够帮我。
代码:
def read_mail_from_pop3_server():
try:
conn = poplib.POP3_SSL(EMAIL_SERVER, 995)
print('Logging into Office365')
conn.user(EMAIL_USER)
conn.pass_(EMAIL_PASSWORD)
mail_count = len(conn.list()[1])
print("You have %d email messages." % mail_count)
output = []
for i in range(mail_count):
raw_email = b"\n".join(conn.retr(i + 1)[1])
msg = email.message_from_string(raw_email)
items_list = msg.items()
dict_items = OrderedDict(items_list)
email_body = ''
if msg.is_multipart():
...
else:
...
dict_items.update({'Body': email_body})
dict_obj = TatvamObjDict.load_fromFile('MailJsonData.Json')
dict_obj.review.original_text = dict_items['Body'].lstrip() if 'Body' in dict_items else "".strip()
...
if "<xyz@gmail.com>" in dict_items.itervalues():
output.append(dict_obj)
with open('del_check.json', 'w') as f:
json.dump(output, f, indent=4, sort_keys=True)
f.write('\n')
# # deleting emails directly by email index(count)
conn.dele(mail_count)
conn.quit()