

使用 Python 读取 Outlook 中电子邮件的方法
source link: https://www.linuxprobe.com/python-outlook-read.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

从事电子邮件营销,准入opt-in邮箱列表是必不可少的。你可能已经有了准入列表,同时还使用电子邮件客户端软件。如果你能从电子邮件客户端中导出准入列表,那这份列表想必是极好的。
我使用一些代码来将 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.SenderEmailAddress if 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.Folders for 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.Folders for 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.SenderEmailAddress if sender != "": print(sender, file=f) except: print("Error") 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.Folders for 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.Folders for y in x: emailleri_al(y) print("****Folder Name**********************************", file=f) print("..."+y.name,file=f) print("*************************************************", file=f) print("Finished Succesfully")
via: https://www.codementor.io/aliacetrefli/how-to-read-outlook-emails-by-python-jkp2ksk95
作者:A.A. Cetrefli 选题:lujun9972 译者:lujun9972 校对:wxy
Recommend
-
61
这是关于c#发送电子邮件(SMTP)的技术笔记,以”简报“形式呈现。 因为最后成功通过outlook.com发送了邮件,所以,我觉得还是有必要 记录一下其中的要点。 一、技术核心 .net Framework 提供的两个类:SmtpCl...
-
12
你不需要使用任何后端语言,如 PHP 或 Python。此外,你甚至不需要Node.js! 有很多方法可以读取这些数据。你可以将你的表单与数据库(如MySQL)连接,然后从数据库中读取传入的信息。好吧,这是一个选择,但是我认为这对于你的非技...
-
18
如何使用Python读取GeoJSON和Shapefile文件,并转换坐标系上海市新能源汽车公共数据采集与监测研究中心 数据分析师下面这个网页可以不写代码进行Shapefile和GeoJSON的坐标系转...
-
6
使用Emacs发送电子邮件 Emacs自带了SMTP库,这使的Emacs无需任何第三方软件的帮助就能通过第三方邮件服务商发送电子邮件. 在写邮件之前,推荐先配置一下 user-full-name 与 user-mail-address 这两...
-
5
使用 Python 读取火狐的 cookies 本文来自依云's Blog,转载请...
-
8
#yyds干货盘点# 盘点两种使用Python读取.nc文件的方法 原创 Python进阶者 2022-01-16 21:53...
-
7
微软:Outlook将于明年底不再向个人提供个性化电子邮件地址服务
-
5
Table of Contents1 迭代器的概念 迭代器(iterator)有时又称光标(cursor)是程序设计的软件设计模式,可在容器对象(container,例如链表或数组)上遍访的接口。 2 迭代器的特点 强制性:必须将...
-
7
新版Outlook将在2024年到来,成为新装Win 11的默认电子邮件应用程序
-
8
最快的 Python 读取 Excel 方法 我没有任何数据来支持接下来的说法,但我相当肯定,Excel 是存储、操作,甚至是传递数据的最常见方式(!)。这就是为什么在 Python 中读取 Excel 的原因。我最近需要用 Python...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK