阿里邮箱IMAP收信代码示例

IMAP收信代码示例

Python3:

# -*- coding:utf-8 -*-
import imaplib
import email
from imapclient import imap_utf7


def get_folders():
    # 获取邮箱文件夹列表
    list_folder = []
    list_folder_org = conn.list()
    for i in list_folder_org[1]:
        list_folder.append(imap_utf7.decode(i).split('/" ')[1].replace('"', ''))  # 截取文件夹名称
    print(list_folder)
    return list_folder


def get_content(msg):
    # 解码邮件内容
    if msg.is_multipart():
        # 列表
        return get_content(msg.get_payload(0))  # 获取邮件中的文本
    else:
        # 字符串
        return msg.get_payload(None,
                               decode=True)  # 获取邮件中的文本,如果是True将解码base64/quoted-printable等格式编码内容,否则不解码


host = 'imap.qiye.aliyun.com'
user = 'xxxxxx'
pwd = 'xxxxxx'

# 连接服务器,并登录
# conn = imaplib.IMAP4(host, 143)
conn = imaplib.IMAP4_SSL(host, 993)  # SSL加密连接

# 认证登录
conn.login(user, pwd)

print('邮箱文件夹列表:')
get_folders()  # 获取邮箱文件夹列表

folder_name = imap_utf7.encode('INBOX')  # utf-7编码,中文编码
conn.select(folder_name)  # 'draft','INBOX'   #选取文件夹
v_type, data = conn.search(None, 'UNSEEN SINCE 05-Mar-2022')  # 查询某日期后的未读邮件

email_list = data[0].split()
if len(email_list) == 0:
    print('文件夹为空,退出!')
    exit(1)

print('状态=', v_type, '消息队列序号num=', data)
print('该文件夹下所有的UID:', conn.uid('search', None, "ALL"))
# 获取最后一封邮件的序号
item = email_list[len(email_list) - 1]
print('最后一封邮件的消息队列序号num:', item)

v_status = conn.status(folder_name, '()')
v_status_1 = v_status[1][0].decode('utf-8').replace('MESSAGES', '邮箱中的邮件总数:').replace('RECENT',
                                                                                     '邮箱中标志为\RECENT的邮件数:').replace(
    'UIDNEXT', '可以分配给新邮件的下一个UID:').replace('UIDVALIDITY', '邮箱的UID有效性标志:').replace('UNSEEN', '未读邮件:')

print('邮箱文件夹基本信息:', folder_name, v_status_1)
v_count = 1
list_show_yulan = []

# 解析邮件
for num in email_list:
    v_type, data = conn.fetch(num, '(RFC822)')
    UID = conn.fetch(num, 'UID')[1]
    v_num_uid = UID[0].decode('utf-8').replace('(', '').replace(')', '').split(' ')

    print('\n解析第' + str(v_count) + '封邮件,num=', v_num_uid[0], ' UID=', v_num_uid[2],
          '===============================================')

    print('服务器返回值=', data[0])  # 返回的结果,包含num,UID和邮件原文
    msg = email.message_from_bytes(data[0][1])
    print('邮件eml原文:\n', msg)
    v_count = v_count + 1

conn.print_log()  # 打印IMAP日志
conn.close()