python读取微软邮箱的验证码

1首先得知道邮箱的账号和密码
2 开头smtp pop
代码奉上

# 读取邮件信息获取验证码
def recv_email_by_pop3(email_address, password):
    import imaplib
    # 这里的服务器根据需要选择
    server = imaplib.IMAP4_SSL("outlook.office365.com",993)
    server.login(email_address, password)
    # 邮箱中的文件夹,默认为'INBOX'
    try:
        inbox = server.select("INBOX")
        typ, data = server.search(None, "ALL")
        msgList = data[0].split()
        latest = msgList[len(msgList) - 1]
        typ, datas = server.fetch(latest, '(RFC822)')
            # 使用utf-8解码
        msg_content = (b''.join(datas[0]).decode('utf-8')[2600:3200]).replace('\r\n','')
        code = re.findall("x-ds-vetting-token: (.*?)X-DKIM_SIGN_REQUIRED", msg_content)[0]
        server.close()
        return code
    except Exception as e:
        inbox = server.select("Junk")
        typ, data = server.search(None, "ALL")
        msgList = data[0].split()
        latest = msgList[len(msgList) - 1]
        typ, datas = server.fetch(latest, '(RFC822)')
        # 使用utf-8解码
        msg_content = (b''.join(datas[0]).decode('utf-8')[2600:3300]).replace('\r\n', '')
        code = re.findall("x-ds-vetting-token: (.*?)X-DKIM_SIGN_REQUIRED", msg_content)[0]
        server.close()
        return code
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章