2017-04-12 112 views
0

提取收件人的名字,我試圖提取收件人地址,我得到下面的時候我打印:無法在Outlook電子郵件的Python

<COMObject <unknown>> 

我有下面的代碼。我也試過Receipient.Name

import win32com.client 
import sys 
import csv 


outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items 
message = messages.GetLast() 
subject_list = [] 
sender_list = [] 
recipients_list = [] 
i = 0 

while message: 

    subject = message.Subject 
    sender = message.SenderName 
    recipients = message.Recipients 


    subject_list.append(str(subject)) 
    sender_list.append(str(sender)) 
    recipients_list.append(recipients) 
    i+=1 

    message = messages.GetPrevious() 
    if i > 10: 
     break 

for subject in subject_list: 
    print(subject) 
for sender in sender_list: 
    print(sender) 
for recipient in recipients_list: 
    print(recipient)  

如何獲取收件人姓名或電子郵件地址?

回答

1

我認爲「message.Recipients」會讓你成爲Recipient Collection,而不是特定的收件人。您可以通過將集合中的單個收件人添加到您的列表中來解決此問題。

要獲取電子郵件地址,如果地址將地址作爲Exchange用戶提供給您,則可能需要一些額外的導航,因此您可以使用Address Entry中的函數來檢索它。 (您可以通過瀏覽這些鏈接中的msdn文檔來了解關於這些Outlook對象的更多信息。)

我在代碼中添加了幾行代碼,以下代碼適用於我。

while message: 

    subject = message.Subject 
    sender = message.SenderName 
    recipients = message.Recipients 

    subject_list.append(str(subject)) 
    sender_list.append(str(sender)) 
    #This loop will add each recipient from an email's Recipients Collection to your list individually 
    for r in recipients: 
     recipients_list.append(r) 
    i+=1 

    message = messages.GetPrevious() 
    if i > 10: 
     break 

for subject in subject_list: 
    print(subject) 
for sender in sender_list: 
    print(sender) 
for recipient in recipients_list: 
    #Now that the for loop has gone into the Recipient Collection, this should print the recipient name 
    print(recipient) 
    #This is the "Address", but for me they all appeared as Exchange Users, rather than explicit email addresses. 
    print(recipient.Address) 
    #I used this code to get the actual addresses 
    print(recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress)