【问题标题】:Extract sender's email address from Outlook Exchange in Python using win32使用win32从Python中的Outlook Exchange中提取发件人的电子邮件地址
【发布时间】:2015-10-15 15:02:49
【问题描述】:

我正在尝试使用 python 中的 win32 包从 Outlook 2013 中提取发件人的电子邮件地址。我的收件箱中有两种电子邮件地址类型,exchange 和 smtp。如果我尝试打印 Exchange 类型的发件人电子邮件地址,我会得到:

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=6F467C825619482293F429C0BDE6F1DB-

我已经浏览过这个link,但找不到可以提取 smtp 地址的函数。

下面是我的代码:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders
for msg in all_inbox:
   print msg.SenderEmailAddress  

目前所有的电子邮件地址都是这样的:

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=6F467C825619482293F429C0BDE6F1DB-

我在 VB.net link 中找到了解决方案,但不知道如何在 Python 中重写相同的东西。

【问题讨论】:

    标签: python email outlook pywin32 win32com


    【解决方案1】:

    我今天在使用 win32com 时遇到了同样的问题。我找到了解决方案here

    使用您的示例将是:

    from win32com.client import Dispatch
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder("6")
    all_inbox = inbox.Items
    folders = inbox.Folders
    
    for msg in all_inbox:
       if msg.Class==43:
           if msg.SenderEmailType=='EX':
               if msg.Sender.GetExchangeUser() != None:
                   print msg.Sender.GetExchangeUser().PrimarySmtpAddress
               else:
                   print msg.Sender.GetExchangeDistributionList().PrimarySmtpAddress
           else:
               print msg.SenderEmailAddress
    

    这应该可以解决群组邮件问题。

    【讨论】:

      【解决方案2】:

      首先,如果文件夹中有MailItem以外的项目,例如ReportItemMeetingItem等,您的代码将失败。您需要检查Class属性。

      其次,您需要检查发件人电子邮件地址类型,并将 SenderEmailAddress 仅用于“SMTP”地址类型。在 VB 中:

       for each msg in all_inbox
         if msg.Class = 43 Then
           if msg.SenderEmailType = "EX" Then
             print msg.Sender.GetExchangeUser().PrimarySmtpAddress
           Else
             print msg.SenderEmailAddress 
           End If  
         End If
       next
      

      【讨论】:

      • 谢谢兄弟 :) 你节省了我很多时间。
      【解决方案3】:

      我只是在 Python 中修改上面给出的程序。

      from win32com.client import Dispatch
      outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
      inbox = outlook.GetDefaultFolder("6")
      all_inbox = inbox.Items
      folders = inbox.Folders
      
      for msg in all_inbox:
             if msg.Class==43:
                 if msg.SenderEmailType=='EX':
                     print msg.Sender.GetExchangeUser().PrimarySmtpAddress
                 else:
                     print msg.SenderEmailAddress
      

      这只会打印出您收件箱文件夹中所有发件人的电子邮件地址。

      【讨论】:

      • 在我的例子中,AttributeError: 'NoneType' object has no attribute 'PrimarySmtpAddress' 失败了。怎么可能没有PrimarySmtpAddres?
      • 很可能,您的文件夹在测试时不包含任何电子邮件。
      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 2022-01-13
      • 2021-03-08
      • 2011-12-17
      • 2019-01-09
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多