我正在嘗試使用 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()
如您所見,我可以獲得發件人的電子郵件地址……但是我如何獲得收件人的電子郵件地址?
uj5u.com熱心網友回復:
它類似于您對發件人所做的事情 - 回圈遍歷MailItem.Recipients集合中的收件人,并為每個人Recipient使用該Recipient.AddressEntry屬性來執行您已經對該MailItem.Sender屬性執行的操作。
另請注意,這不是最有效的方法 - 如果組態檔沒有父 Exchange 服務器,則打開地址條目可能會很昂貴或完全不可能,例如,如果您正在處理獨立的 MSG 檔案或從交換郵箱。在大多數情況下,SMTP 地址可直接在郵件中使用,例如來自PidTagSenderSmtpAddress(DASL 名稱http://schemas.microsoft.com/mapi/proptag/0x5D01001F),可使用 訪問MailItem.PropertyAccessor.GetProperty。同樣,收件人 SMTP 地址可能在PR_SMTP_ADDRESS屬性(DASL name http://schemas.microsoft.com/mapi/proptag/0x39FE001F,use Recipient.PropertyAccessor.GetProperty)中可用 - 您可以在OutlookSpy 中看到這些屬性(單擊 IMessage 按鈕屬性)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383257.html
