我正在尝试使用 Win32com 客户端在 Python 中提取收件人电子邮件地址。
这是我到目前为止的代码:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["[my email address"].Folders["Inbox"]
def get_email_address():
for message in inbox.Items:
print("========")
print("Subj: " + message.Subject)
print('To:', message.Recipients) #this part does not work
print("Email Type: ", message.SenderEmailType)
if message.Class == 43:
try:
if message.SenderEmailType == "SMTP":
print("Name: ", message.SenderName)
print("Email Address: ", message.SenderEmailAddress)
print('To:', message.Recipients) #this part does not work
print("Date: ", message.ReceivedTime)
elif message.SenderEmailType == "EX":
print("Name: ", message.SenderName)
print("Email Address: ", message.Sender.GetExchangeUser(
).PrimarySmtpAddress)
print('To:', message.Recipients) #this part does not work
print("Date: ", message.ReceivedTime)
except Exception as e:
print(e)
continue
if __name__ == '__main__':
get_email_address()
如您所见,我可以获取发件人电子邮件地址...但如何获取收件人电子邮件地址?
最佳答案
这与您对发件人所做的类似 - 循环遍历 MailItem.Recipients
集合中的收件人,并为每个 Recipient
使用 Recipient.AddressEntry
属性来执行您已经使用 MailItem.Sender
属性执行的操作。
另请注意,这不是最有效的方法 - 如果配置文件没有父 Exchange 服务器,例如,打开地址条目的成本可能会很高,甚至根本不可能。如果您正在处理独立的 MSG 文件或从 Exchange 邮箱复制到 PST 的邮件。在大多数情况下,SMTP 地址可直接在邮件中使用,例如来自 PidTagSenderSmtpAddress
(DASL 名称 http://schemas.microsoft.com/mapi/proptag/0x5D01001F
),可以使用 MailItem.PropertyAccessor.GetProperty< 进行访问
。同样,收件人 SMTP 地址可能在 PR_SMTP_ADDRESS
属性中可用(DASL 名称 http://schemas.microsoft.com/mapi/proptag/0x39FE001F
,使用 Recipient .PropertyAccessor.GetProperty
) - 您可以在 OutlookSpy 中看到这些属性(我是它的作者 - 单击 IMessage 按钮属性)。
关于python - 使用 Python Win32com 在 Outlook 中提取收件人电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70276607/