【发布时间】:2022-01-13 11:58:24
【问题描述】:
我正在尝试使用 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()
如您所见,我可以获取发件人电子邮件地址...但是如何获取收件人电子邮件地址?
【问题讨论】:
-
Recipients是Recipient对象的集合,而不是电子邮件地址列表。 docs.microsoft.com/en-us/office/vba/api/outlook.recipients 您需要遍历它,例如for recip in message.Recipients: print(recip.Address) -
谢谢,但按照您的建议应用以下代码只会给我收件人姓名,而不是收件人电子邮件地址。对于收件箱中的消息。项目:对于消息中的 x。收件人:打印(x.AddressEntry)
标签: python email outlook win32com