简体   繁体   English

如何从Outlook 2013中未读的电子邮件中下载附件?

[英]How do I download attachments from unread emails in Outlook 2013?

I'm trying to download all of the attachments of the unread messages in a specific folder. 我正在尝试将未读邮件的所有附件下载到特定文件夹中。 As a test, I tried to loop print the subject line of each unread message, but am only getting the top email's. 作为测试,我尝试循环打印每条未读邮件的主题行,但只得到顶部电子邮件。 Please help. 请帮忙。 Also, is there a way to mark the message as read? 另外,有没有办法将邮件标记为已读?

Thanks, 谢谢,

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders('The Machine').Folders('Delivered Assets').Folders('Daily')
messages = inbox.Items
message = messages.GetLast()
attachments = message.Attachments
attachment = attachments.Item(1)
#attachment.SaveAsFile('C:\\temp\\' + attachment.FileName) #this downloads the attachment to specified path

for item in messages: 
    if item.Unread==True:
        print message.Subject #this only prints the top email's subject

First of all, you need to iterate over all unread emails in the folder. 首先,您需要遍历文件夹中所有未读的电子邮件。 To get this done you need to use the Find / FindNext or Restrict methods of the Items class. 为此,您需要使用Items类的Find / FindNextRestrict方法。 You can read more about these methods and find the sample code in the following articles: 您可以阅读有关这些方法的更多信息,并在以下文章中找到示例代码:

For example, in C# it will look like the code listed below: 例如,在C#中,它将看起来像下面列出的代码:

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);
}
finally
{
    if (folderItems != null) Marshal.ReleaseComObject(folderItems);
    if (resultItems != null) Marshal.ReleaseComObject(resultItems);
}

To save the attached file you need to use the SaveAsFile method of the Attachment class. 若要保存附件,您需要使用Attachment类的SaveAsFile方法。 The Attachments property of the MailItem class returns an Attachments object that represents all the attachments for the specified item. MailItem类的Attachments属性返回一个Attachments对象,该对象表示指定项目的所有附件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM