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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章