This question already has answers here:
Wrong mailbox items being retrieved using Exchange Web Services managed API in C#
                                
                                    (2个答案)
                                
                        
                                5年前关闭。
            
                    
我正在尝试阅读一个特定的电子邮件帐户,以查找带有未读附件的邮件,并且该邮件中包含某些单词。

我有以下有效的代码,但仅读取我的收件箱,而不读取我要检查的收件箱

static void Main(string[] args)
    {
    ServicePointManager.ServerCertificateValidationCallback=CertificateValidationCallBack;
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);// .Exchange2007_SP1);

    service.UseDefaultCredentials = true;
    service.AutodiscoverUrl("[email protected]"); //, RedirectionUrlValidationCallback); //[email protected]

    ItemView view = new ItemView(1);

    string querystring = "HasAttachments:true Kind:email";
    //From:'Philip Livingstone' Subject:'ATTACHMENT TEST' Kind:email";

    // Find the first email message in the Inbox that has attachments.
    // This results in a FindItem operation call to EWS.
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);

    foreach (EmailMessage email in results)

        if (email.IsRead == false) // && email.HasAttachments == true
        {
            System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);

            // Request all the attachments on the email message.
            //This results in a GetItem operation call to EWS.
            email.Load(new PropertySet(EmailMessageSchema.Attachments));

            foreach (Attachment attachment in email.Attachments)
            {
                if (attachment is FileAttachment)
...


我必须更改什么才能使我的代码读取[email protected]收件箱中的消息?

最佳答案

默认情况下,在ExchangeService对象上调用FindItems时,邮箱的收件箱文件夹将设置为root。假设您的域帐户具有访问其他邮箱的必要权限,则下面的方法可以解决问题。我省略了服务创建部分。

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(@"[email protected]");

//creates a folder object that will point to inbox folder
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);

//load items from mailbox inbox folder
if(inbox != null)
{
    FindItemsResults<Item> items = inbox.FindItems(new ItemView(100));

    foreach(var item in items)
        Console.WriteLine(item);
}

10-07 22:37