如何用Python读取Outlook中的电子邮件

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

https://s4.51cto.com/oss/201808/24/ffaf6a4c3fe3af850e245c685cc467e8.jpeg

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

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

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

  1. pip install pywin32 

我们需要通过 MAPI 协议连接 Outlok:

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

然后从 outlook 配置中获取所有的账户:

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

在然后需要从名为 emaileri_al 的收件箱中获取邮件:

  1. def emailleri_al(folder): 
  2.     messages = folder.Items 
  3.     a=len(messages) 
  4.     if a>0: 
  5.         for message2 in messages: 
  6.              try: 
  7.                 sender = message2.SenderEmailAddress 
  8.                 if sender != ""
  9.                     print(sender, file=f) 
  10.              except
  11.                 print("Ben hatayım"
  12.                 print(account.DeliveryStore.DisplayName) 
  13.                 pass 
  14.  
  15.              try: 
  16.                 message2.Save 
  17.                 message2.Close(0) 
  18.              except
  19.                  pass 

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

  1. for account in accounts: 
  2.     global inbox 
  3.     inbox = outlook.Folders(account.DeliveryStore.DisplayName) 
  4.     print("****Account Name**********************************",file=f) 
  5.     print(account.DisplayName,file=f) 
  6.     print(account.DisplayName) 
  7.     print("***************************************************",file=f) 
  8.     folders = inbox.Folders 
  9.  
  10.     for folder in folders: 
  11.         print("****Folder Name**********************************", file=f) 
  12.         print(folder, file=f) 
  13.         print("*************************************************", file=f) 
  14.         emailleri_al(folder) 
  15.         a = len(folder.folders) 
  16.  
  17.         if a>0 : 
  18.             global z 
  19.             z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name
  20.             x = z.Folders 
  21.             for y in x: 
  22.                 emailleri_al(y) 
  23.                 print("****Folder Name**********************************", file=f) 
  24.                 print("..."+y.name,file=f) 
  25.                 print("*************************************************", file= 

下面是完整的代码:import win32com.client 

  1. import win32com 
  2. import os 
  3. import sys 
  4.  
  5. f = open("testfile.txt","w+"
  6.  
  7. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI"
  8. accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts; 
  9.  
  10. def emailleri_al(folder): 
  11.     messages = folder.Items 
  12.     a=len(messages) 
  13.     if a>0: 
  14.         for message2 in messages: 
  15.              try: 
  16.                 sender = message2.SenderEmailAddress 
  17.                 if sender != ""
  18.                     print(sender, file=f) 
  19.              except
  20.                 print("Error"
  21.                 print(account.DeliveryStore.DisplayName) 
  22.                 pass 
  23.  
  24.              try: 
  25.                 message2.Save 
  26.                 message2.Close(0) 
  27.              except
  28.                  pass 
  29.  
  30.  
  31.  
  32. for account in accounts: 
  33.     global inbox 
  34.     inbox = outlook.Folders(account.DeliveryStore.DisplayName) 
  35.     print("****Account Name**********************************",file=f) 
  36.     print(account.DisplayName,file=f) 
  37.     print(account.DisplayName) 
  38.     print("***************************************************",file=f) 
  39.     folders = inbox.Folders 
  40.  
  41.     for folder in folders: 
  42.         print("****Folder Name**********************************", file=f) 
  43.         print(folder, file=f) 
  44.         print("*************************************************", file=f) 
  45.         emailleri_al(folder) 
  46.         a = len(folder.folders) 
  47.  
  48.         if a>0 : 
  49.             global z 
  50.             z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name
  51.             x = z.Folders 
  52.             for y in x: 
  53.                 emailleri_al(y) 
  54.                 print("****Folder Name**********************************", file=f) 
  55.                 print("..."+y.name,file=f) 
  56.                 print("*************************************************", file=f)  
  57.   
  58. print("Finished Succesfully")  

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2020-05-25 14:32:42

Python电子邮件自动化

2019-06-26 18:30:30

Linux命令行电子邮件

2020-01-09 20:14:44

Windows 10Outlook电子邮件

2011-09-20 09:56:13

Google微软电子邮件

2019-08-08 14:55:19

电子邮件微软信头

2010-09-17 14:11:18

2021-06-28 21:21:54

电子邮件邮件安全恶意软件

2021-06-16 10:56:32

电子邮件电子邮件攻击BEC

2010-10-21 13:54:20

2023-03-01 09:48:45

2010-12-28 10:08:27

2022-04-29 15:55:51

安全电子邮件病毒

2011-08-01 11:11:55

2010-12-27 16:22:27

邮件地址

2014-09-09 16:44:16

2009-10-14 10:06:03

电子邮件安全关联 网络安全

2011-12-15 10:45:33

2010-06-10 14:10:58

安全电子邮件协议

2021-08-27 11:11:01

电子邮件邮件安全网络钓鱼

2011-08-01 12:43:03

点赞
收藏

51CTO技术栈公众号