温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python怎么读取Outlook电子邮件

发布时间:2022-01-25 09:26:11 来源:亿速云 阅读:438 作者:iii 栏目:开发技术

这篇文章主要介绍“Python怎么读取Outlook电子邮件”,在日常操作中,相信很多人在Python怎么读取Outlook电子邮件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python怎么读取Outlook电子邮件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

从事电子邮件营销,准入opt-in邮箱列表是必不可少的。你可能已经有了准入列表,同时还使用电子邮件客户端软件。如果你能从电子邮件客户端中导出准入列表,那这份列表想必是极好的。

Python怎么读取Outlook电子邮件

我使用一些代码来将 outlook 配置中的所有邮件写入一个临时文件中,现在让我来尝试解释一下这些代码。

首先你需要导入 win32com.client,为此你需要安装 pywin32:

pip install pywin32

我们需要通过 MAPI 协议连接 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

你需要进入所有账户的所有收件箱中获取电子邮件:

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")

到此,关于“Python怎么读取Outlook电子邮件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI