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

从事电子邮件营销,准入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")  

网页标题:如何用Python读取Outlook中的电子邮件
URL地址:http://www.hantingmc.com/qtweb/news19/435519.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联