【问题标题】:Delete Emails from Outlook Folder Using C#使用 C# 从 Outlook 文件夹中删除电子邮件
【发布时间】:2017-06-16 09:42:14
【问题描述】:

我似乎无法让它工作。我正在尝试遍历我创建的名为“SlaughterPDFs”的文件夹中的所有邮件项目并删除电子邮件。

下面是我正在使用的代码。在这段代码中,我只是试图从 Outlook 的“草稿”文件夹中删除邮件项目。

public void deleteMails()
{
    Application tempApp = new Application();
    MAPIFolder tempInbox = default(MAPIFolder);
    Items JunkItems = default(Items);

    tempInbox = tempApp.GetNamespace("MAPI").
        GetDefaultFolder(OlDefaultFolders.olFolderDrafts);
    JunkItems = tempInbox.Items;
    MailItem DeleteMail = default(MailItem);
    foreach (object newMail_loopVariable in JunkItems)
    {
        DeleteMail = (MailItem)newMail_loopVariable;
        DeleteMail.Delete();
    }
    JunkItems = null;
    tempInbox = null;
    tempApp = null;
}

有人知道我做错了什么吗?或者我应该将这些电子邮件移动到新文件夹。

【问题讨论】:

    标签: c# c#-4.0 outlook delete-file


    【解决方案1】:

    我不确定您在哪里遇到问题。 如果你用下面的 while 循环替换你的 for 循环,它应该删除文件夹中的所有电子邮件。

    while (tempInbox.Items.Count > 0)
    {
        DeleteMail = (MailItem)tempInbox.Items.GetFirst();
        DeleteMail.Delete();
    }
    

    如果您无法访问我将使用的文件夹(假设 SlaughterPDFs 是收件箱的子文件夹):

    tempInbox = tempApp.GetNamespace("MAPI").
                     GetDefaultFolder(OlDefaultFolders.olFolderInbox);
    tempInbox = tempInbox.Folders["SlaughterPDFs"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多