簡體   English   中英

通過 python 轉發來自 outlook 中特定發件人的電子郵件

[英]Forward emails from a specifc sender in outlook via python

我嘗試了下面的代碼,但屬性錯誤讓我發瘋。 消息后的任何事情都會給我一個屬性錯誤。 wtv 道具。 我用。 我最終遇到了屬性錯誤。

AttributeError: <unknown>.Sender

代碼:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the inbox
messages = inbox.Items

sender_email = "TDC@AE.Roco.COM"
recipient_email = "simple.invoice@net"

for message in messages:
    if message.Sender.Address == sender_email:
        new_mail = message.Forward()
        new_mail.Recipients.Add(recipient_email)
        for attachment in message.Attachments:
            new_mail.Attachments.Add(attachment)
        new_mail.Save()

根據給出的答案:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
accounts = mapi.Folders

query = '@SQL="urn:schemas:httpmail:from" = ' + "'TDC@AE.Roco.COM'" + ' AND "urn:schemas:httpmail:hasattachment" = ' + "'1'"
print(query)

try:
    items = inbox.Items.Restrict(query)
    print(f'Number of items found : {items.count}')


    def check_subfolders(folder):
        items = folder.Items.Restrict(query)
        if items.count > 0:
            print(f'{items.count} emails found in {folder.name}')
        for subfolder in folder.Folders:
            check_subfolders(subfolder)


    check_subfolders(inbox)
    for folder in mapi.Folders:
        items = folder.Items.Restrict(query)
        if items.count > 0:
            print(f'{items.count} emails found in {folder.name}')
    for item in items:
        mail = item.Forward()
        mail.Recipients.Add("simple.invoice@net")
        mail.Subject = "Fwd: " + item.Subject
        mail.Body = "Please find the forwarded message with attachments below:\n\n" + item.Body
        mail.Save()
except Exception as e:
    print(f'An error occurred: {e}')

現在我沒有錯誤,但結果返回零,盡管我有來自指定發件人的郵件!

並非所有 Outlook 項都提供Sender屬性。 Outlook文件夾可能包含不同類型的項目- 筆記、日歷項目、郵件項目、文檔項目等。因此,確保您處理郵件項目是有意義的,例如,您可以檢查MessageClass屬性。

而不是遍歷 Outlook 中的所有項目:

for message in messages:
    if message.Sender.Address == sender_email:

使用Items class 的Find / FindNextRestrict方法。它們允許獲取與搜索條件相對應的項目,因此您可以迭代它們而無需每次檢查屬性。 在我為技術博客撰寫的文章中閱讀有關這些方法的更多信息:

使用以下搜索條件從指定的 email 地址獲取項目:

criteria = "@SQL=" & Chr(34) _ 
& "urn:schemas:httpmail:senderemail" & Chr(34) _ 
& " = 'eugene@astafiev.com'"

如果您需要從特定發件人發送郵件,您可以使用MailItem.SendUsingAccount屬性返回或設置一個Account object,它表示要發送MailItem的帳戶。 注意,other賬號應該配置在Outlook中。例如VBA中的示例展示了如何使用它:

Sub SendUsingAccount() 
 Dim oAccount As Outlook.account 
 For Each oAccount In Application.Session.Accounts 
   If oAccount.AccountType = olPop3 Then 
     Dim oMail As Outlook.MailItem 
     Set oMail = Application.CreateItem(olMailItem) 
     oMail.Subject = "Sent using POP3 Account" 
     oMail.Recipients.Add ("someone@example.com") 
     oMail.Recipients.ResolveAll 
     Set oMail.SendUsingAccount = oAccount 
     oMail.Send 
   End If 
 Next 
End Sub

另一種方法是使用MailItem.SentOnBehalfOfName屬性,該屬性返回或設置一個字符串,指示郵件消息的預期發件人的顯示名稱。 在這種情況下,其他用戶應該有權代表 Exchange 中的另一個人發送。

Sender屬性僅由MailItem object 公開,但您也可以在收件箱文件夾中包含ReportItemMeetingItem對象。 您需要先檢查Class屬性 == 43 (即olMail

此外,不要遍歷文件夾中的所有項目 - 使用Items.Find/FindNextItems.Restrict查詢[SenderEmailAddress] = 'TDC@AE.Roco.COM'

快速示例

import win32com.client

def outlook_emails(Inbox):
    Filter_sender = "@SQL=""urn:schemas:httpmail:fromemail"" " \
                    "ci_phrasematch 'TDC@AE.Roco.COM'"

    items = Inbox.Items.Restrict(Filter_sender)
    print(items.Count)

    for i in reversed(range(items.Count, 0, -1)):
        if items[i].Class == 43:
            print(items[i].Subject)


if __name__ == "__main__":
    outlook = win32com.client.Dispatch(
        "outlook.Application").GetNamespace(
        "MAPI"
    )

    inbox = outlook.GetDefaultFolder(6)
    outlook_emails(inbox)

@SQL=""urn:schemas:httpmail:fromemail"" ci_phrasematch

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM