繁体   English   中英

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

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

我正在尝试将未读邮件的所有附件下载到特定文件夹中。 作为测试,我尝试循环打印每条未读邮件的主题行,但只得到顶部电子邮件。 请帮忙。 另外,有没有办法将邮件标记为已读?

谢谢,

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

首先,您需要遍历文件夹中所有未读的电子邮件。 为此,您需要使用Items类的Find / FindNextRestrict方法。 您可以阅读有关这些方法的更多信息,并在以下文章中找到示例代码:

例如,在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);
}

若要保存附件,您需要使用Attachment类的SaveAsFile方法。 MailItem类的Attachments属性返回一个Attachments对象,该对象表示指定项目的所有附件。

暂无
暂无

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

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