[英]How to access emails sender in Outlook using Pywin32
I would like to print out some information about emails that have Category="Backlog" for instance.例如,我想打印一些有关具有 Category="Backlog" 的电子邮件的信息。 I use pywin32 library as below code:
我使用pywin32库如下代码:
import win32com.client
import os, sys
from datetime import date, datetime, timedelta
def print_messages(messages, sCategory, f):
msgs = messages.Restrict("[Categories] = '" + sCategory + "'")
for message in list(msgs):
try:
if message.Class == 43:
if message.SenderEmailType == "EX":
strSender = message.Sender.GetExchangeUser().PrimarySmtpAddress
else:
strSender = message.SenderEmailAddress
f.write(message.EntryID + "\t" + strSender + message.ConversationTopic + "\t" + message.ReceivedTime.strftime("%Y-%m-%d %H:%M") + "\t" + message.Categories + "\n")
except Exception as e:
print("Oops!", sys.exc_info()[0], "occurred.")
def main():
outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
now = datetime.now()
f = open("C:/Temp/outlook_emails_" + now.strftime("%Y-%m-%d_%H-%M-%S") + ".txt", "a")
print_messages(messages, "Backlog", f)
f.close()
if __name__ == "__main__":
main()
When this code run, it can't get the email sender.当此代码运行时,它无法获取 email 发件人。 Exception happens and it prints lines like:
发生异常并打印如下行:
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.
The issue in lines: strSender = message.Sender.GetExchangeUser().PrimarySmtpAddress
and in line strSender = message.SenderEmailAddress
.行中的问题:
strSender = message.Sender.GetExchangeUser().PrimarySmtpAddress
和行strSender = message.SenderEmailAddress
。 Not sure how to correctly access the sender email address!!不知道如何正确访问发件人 email 地址!!
Versions used:使用的版本:
You never check that GetExchangeUser
returns a valid object rather than null.您永远不会检查
GetExchangeUser
返回有效的 object 而不是 null。 The current profile must contain the original Exchange server that houses the GAL user.当前配置文件必须包含容纳 GAL 用户的原始 Exchange 服务器。
Before using GetExchangeUser
, check if PidTagSenderSmtpAddress
MAPI property is available using message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001F")
在使用
GetExchangeUser
之前,使用message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001F")
检查PidTagSenderSmtpAddress
MAPI 属性是否可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.