none
EWS - Folder.Bind Throws ArgumentNullException RRS feed

  • Question

  • I'm trying to create an email client that can give me a list of emails from a certain date using the EWS Managed API but I keep getting an exception when I call Folder.Bind().

    Exception thrown: 'System.ArgumentNullException' in mscorlib.dll Additional information: Value cannot be null.

    Stack

    It says it's an ArgumentNullException but one parameter is an enum and the other is not null at runtime. From the stack it seems to be an exception from within the EWS API that wasn't handled nicely but I don't know what the problem is.

    Here's my code.

    using Microsoft.Exchange.WebServices.Data;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Integration
    {
        public class ExchangeEmailClient : EmailClientBase
        {
            public ExchangeEmailClient(string emailAddress, string password)
            {
                ExchangeService = new ExchangeService();
                ExchangeService.Credentials = new WebCredentials(emailAddress, password);
                ExchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
            }
    
            public ExchangeService ExchangeService { get; private set; }
    
            public override IEnumerable<Email> Receive(DateTime getFrom)
            {
                //Get emails
                var inbox = Folder.Bind(ExchangeService, WellKnownFolderName.Inbox); //Exception thrown here!
                var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, getFrom));
                var emails = inbox.FindItems(filter, new ItemView(int.MaxValue));
    
                //Transform (use data transformation tool?)
                foreach (EmailMessage exchangeEmail in emails)
                {
                    var standardEmail = new Email();
                    standardEmail.Body = exchangeEmail.Body;
                    standardEmail.Subject = exchangeEmail.Subject;
                    standardEmail.From = exchangeEmail.From.Address;
    
                    standardEmail.Attachments = exchangeEmail.Attachments.Select(x => new Uri(x.ContentLocation)).ToList();
                    standardEmail.CarbonCopy = exchangeEmail.CcRecipients.Select(x => x.Address).ToList();
    
                    yield return standardEmail;
                }
            }
    
            public override void Send(Email email)
            {
                var outgoingMail = new EmailMessage(ExchangeService);
                outgoingMail.Body = email.Body;
                outgoingMail.Subject = email.Subject;
                outgoingMail.From = new EmailAddress(email.From);
    
                //Get attachments
                email.Attachments.ForEach(x => outgoingMail.Attachments.AddFileAttachment(x.ToString()));
    
                //Set addresses
                email.To.ForEach(x => outgoingMail.ToRecipients.Add(new EmailAddress(x)));
                email.CarbonCopy.ForEach(x => outgoingMail.CcRecipients.Add(new EmailAddress(x)));
                email.BlindCarbonCopy.ForEach(x => outgoingMail.BccRecipients.Add(new EmailAddress(x)));
    
                //Send
                outgoingMail.SendAndSaveCopy();
            }
        }
    }

    Getting the items like this throws the same exception:

    //Get emails 
    var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, getFrom)); 
    var emails = ExchangeService.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(int.MaxValue)); 

    This happens in both version 2.2.0 and 1.2.0 of the EWS API SDK.


    Tuesday, February 7, 2017 10:51 PM

All replies

  • Hi The Pademelon,

    Like your title mentioned, your case more related to EWS. I will help move your case to Exchange ServerExchange Server Development   forum for better support.

    Best regards,

    Kristin Xie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 8, 2017 2:50 AM
  • Hi

    You must specify the domain name and the exchange version during the initialization of the ExchangeService instance
    Here is a small example

    ExchangeService

    Best regards

    Wednesday, February 8, 2017 8:44 AM