溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python怎么讀取Outlook電子郵件

發(fā)布時(shí)間:2022-01-25 09:26:11 來源:億速云 閱讀:461 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python怎么讀取Outlook電子郵件”,在日常操作中,相信很多人在Python怎么讀取Outlook電子郵件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么讀取Outlook電子郵件”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

從事電子郵件營銷,準(zhǔn)入opt-in郵箱列表是必不可少的。你可能已經(jīng)有了準(zhǔn)入列表,同時(shí)還使用電子郵件客戶端軟件。如果你能從電子郵件客戶端中導(dǎo)出準(zhǔn)入列表,那這份列表想必是極好的。

Python怎么讀取Outlook電子郵件

我使用一些代碼來將 outlook 配置中的所有郵件寫入一個(gè)臨時(shí)文件中,現(xiàn)在讓我來嘗試解釋一下這些代碼。

首先你需要導(dǎo)入 win32com.client,為此你需要安裝 pywin32:

pip install pywin32

我們需要通過 MAPI 協(xié)議連接 Outlok:

outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

然后從 outlook 配置中獲取所有的賬戶:

accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;

在然后需要從名為 emaileri_al 的收件箱中獲取郵件:

def emailleri_al(folder):
messages = folder.Items
a=len(messages)if a>0:for message2 in messages:
try:
sender = message2.SenderEmailAddressif sender != "":print(sender, file=f)
except:print("Ben hatay?m")print(account.DeliveryStore.DisplayName)
pass
try:
message2.Save
message2.Close(0)
except:
pass

你需要進(jìn)入所有賬戶的所有收件箱中獲取電子郵件:

for account in accounts:
global inbox
inbox = outlook.Folders(account.DeliveryStore.DisplayName)print("****Account Name**********************************",file=f)print(account.DisplayName,file=f)print(account.DisplayName)print("***************************************************",file=f)
folders = inbox.Foldersfor folder in folders:print("****Folder Name**********************************", file=f)print(folder, file=f)print("*************************************************", file=f)
emailleri_al(folder)
a = len(folder.folders)if a>0 :
global z
z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name)
x = z.Foldersfor y in x:
emailleri_al(y)print("****Folder Name**********************************", file=f)print("..."+y.name,file=f)print("*************************************************", file=

下面是完整的代碼:

import win32com.client
import win32com
import os
import sys
f = open("testfile.txt","w+")
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;
def emailleri_al(folder):
messages = folder.Items
a=len(messages)if a>0:for message2 in messages:
try:
sender = message2.SenderEmailAddressif sender != "":print(sender, file=f)
except:print("Error")print(account.DeliveryStore.DisplayName)
pass
try:
message2.Save
message2.Close(0)
except:
passfor account in accounts:
global inbox
inbox = outlook.Folders(account.DeliveryStore.DisplayName)print("****Account Name**********************************",file=f)print(account.DisplayName,file=f)print(account.DisplayName)print("***************************************************",file=f)
folders = inbox.Foldersfor folder in folders:print("****Folder Name**********************************", file=f)print(folder, file=f)print("*************************************************", file=f)
emailleri_al(folder)
a = len(folder.folders)if a>0 :
global z
z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name)
x = z.Foldersfor y in x:
emailleri_al(y)print("****Folder Name**********************************", file=f)print("..."+y.name,file=f)print("*************************************************", file=f)print("Finished Succesfully")

到此,關(guān)于“Python怎么讀取Outlook電子郵件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI