簡體   English   中英

Python:保存未讀的所有附件 Outlook email

[英]Python: Save all attachments from unread Outlook email

我在 Outlook 中有一個子文件夾。我的目標是 go 通過該文件夾中所有未讀電子郵件或我今天收到的電子郵件,並將這些電子郵件中的所有現有附件下載到我的桌面上。 到目前為止,我有以下代碼:

def saveattachments(messages,today,path): 
for message in messages:
if message.Unread or message.Senton.date() == today:

        attachments = message.Attachments
        attachment = attachments.Item(1)

        for attachment in message.Attachments:
            attachment.SaveAsFile(os.path.join(path, str(attachment)))
            if message.Unread:
                message.Unread = False
            break



def main():
  path = '\\Desktop\Test Python Save Attachments Outlook'
  today = datetime.today().date()
  outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  inbox = outlook.GetDefaultFolder(6)
  folder = inbox

  folderMessages = folder.Items  
  messages = folderMessages
  saveattachments(messages,today,path)

  print ("Downloading Files successful.") 

if __name__=="__main__":
    main()

上面代碼的問題在於它當時只從 email 下載了一個附件。 此外,它似乎確實比 Excel 個文件更喜歡 PDF 個文件,因為它總是先保存前一個文件。 關於如何相應地更正代碼的任何想法或建議? 提前謝謝了!

你不應該遍歷文件夾中的所有項目——它就像一個沒有 WHERE 子句的 SELECT 查詢。 委婉地說,效率低下。

使用Items.RestrictItems.Find/FindNext查詢范圍內的UnreadSentOn屬性。 您還可以在PR_HASATTACH MAPI 屬性(DASL 名稱"http://schemas.microsoft.com/mapi/proptag/0x0E1B000B" )上添加條件

為確保正確保存所有附加文件,您需要確保將唯一名稱傳遞給SaveAsFile方法。 例如,以下代碼不會檢查目標文件夾中是否已存在此類文件:

for attachment in message.Attachments:
            attachment.SaveAsFile(os.path.join(path, str(attachment)))

我建議使用Attachment class 的FileName屬性,並向文件名添加一個唯一 ID。 請注意,您還需要確保僅將允許的符號用於文件名。 請參閱Windows 和 Linux 目錄名稱中禁止使用哪些字符? 想要查詢更多的信息。

我的目標是 go 通過所有未讀的電子郵件或我今天在該文件夾中收到的電子郵件

正如 Dmitry 所指出的,無需遍歷文件夾中的所有項目。 相反,您只需要找出符合您的條件的項目,然后才對它們進行迭代並保存附件。

要從收件箱文件夾中查找所有未讀項目,您可以使用以下代碼(C#,我不熟悉 python 語法,但 Outlook object model 對於所有類型的應用程序都很常見):

using System.Text;
using System.Diagnostics;
// ...
private void RestrictUnreadItems(Outlook.MAPIFolder folder)
{
    string restrictCriteria = "[UnRead] = true";
    StringBuilder strBuilder = null;
    Outlook.Items folderItems = null;
    Outlook.Items resultItems = null;
    Outlook._MailItem mail = null;
    int counter = default(int);
    object item = null;
    try
    {
        strBuilder = new StringBuilder();
        folderItems = folder.Items;
        resultItems = folderItems.Restrict(restrictCriteria);
        item = resultItems.GetFirst();
        while (item != null)
        {
            if (item is Outlook._MailItem)
            {
                counter++;
                mail = item as Outlook._MailItem;
                strBuilder.AppendLine("#" + counter.ToString() +
                                   "\tSubject: " + mail.Subject);
            }
            Marshal.ReleaseComObject(item);
            item = resultItems.GetNext();
        }
        if (strBuilder.Length > 0)
            Debug.WriteLine(strBuilder.ToString());
        else
            Debug.WriteLine("There is no match in the "
                             + folder.Name + " folder.");
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

Items class 的Find / FindNextRestrict方法可用於此目的。 在以下文章中閱讀有關它們的更多信息:

暫無
暫無

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

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