如何从Outlook读取最新的未读电子邮件,直到我读完邮件为止

时间:2019-07-04 13:11:27

标签: powershell email outlook

我正在尝试使用Powershell从Outlook邮箱中读取未读邮件并显示主题。我想阅读最新的未读电子邮件,直到有任何已读电子邮件为止。在阅读邮件之后,我可能有未读的电子邮件,但不应包括在内。

  $outlook = New-Object -ComObject Outlook.Application
  $namespace = $outlook.GetNameSpace("MAPI")
  $folder=$namespace.GetDefaultFolder(6)
  $folder.Items|ForEach-Object { 
  if($_.Unread -eq $true){
    $_.subject
 }else{
break;
}  
 }   

由于我的收件箱里满是邮件(11011+),所以上面的脚本被卡住了。

2 个答案:

答案 0 :(得分:0)

如果只需要在文件夹中找到未读项目,则需要使用Items类的Find / FindNextRestrict方法。

查找/查找下一个

using System.Text;
using System.Diagnostics;
// ...
private void FindAllUnreadEmails(Outlook.MAPIFolder folder)
{
    string searchCriteria = "[UnRead] = true";
    StringBuilder strBuilder = null;
    int counter = default(int);
    Outlook._MailItem mail = null;
    Outlook.Items folderItems = null;
    object resultItem = null;
    try
    {
        if (folder.UnReadItemCount > 0)
        {
            strBuilder = new StringBuilder();
            folderItems = folder.Items;
            resultItem = folderItems.Find(searchCriteria);
            while (resultItem != null)
            {
                if (resultItem is Outlook._MailItem)
                {
                    counter++;
                    mail = resultItem as Outlook._MailItem;
                    strBuilder.AppendLine("#" + counter.ToString() + 
                                          "\tSubject: " + mail.Subject);
                }
                Marshal.ReleaseComObject(resultItem);
                resultItem = folderItems.FindNext();
            }
            if (strBuilder != null)
                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);
    }
}

限制

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

您可以在以下文章中了解有关这些方法的更多信息:

答案 1 :(得分:0)

您可以使用Restrict()在Outlook中进行预过滤:

...
$folder=$namespace.GetDefaultFolder(6)
$folder.Items.Restrict('[UnRead] = True')|ForEach-Object { 
  $_.subject
}