0

unable to get the emails from outlook by using graph api get method

    public List<Message> GetAllMails()
    {
        List<Message> mails = new List<Message>();
        //var result = graphClientV1.Me.Messages.Request().GetAsync();
        //IUserMessagesCollectionPage currentPage = result.Result;
        IUserMessagesCollectionPage currentPage = graphClientV1.Me.Messages.Request().GetAsync().Result;
        while (currentPage != null)
        {
            foreach (var mail in currentPage)
            {
                try
                {
                    mails.Add(mail);
                }
                catch (Exception) { }
            }
            currentPage = (currentPage.NextPageRequest != null) ? currentPage.NextPageRequest.GetAsync().Result : null;
        }
        return mails;
    }

i want to get the all mails from outlook.

error:

NullReferenceException: Object reference not set to an instance of an object.

6
  • Add some debugging data what is actually null here so we don't need to guess. If this is in the depth of the graph code show the stacktrace of your debugging effort.
    – Ralf
    Commented Jul 1 at 11:35
  • This is the requested URL to get email from graph api: graph.microsoft.com/v1.0/me/messages NULL: currentPage variable is null. error line: IUserMessagesCollectionPage currentPage = graphClientV1.Me.Messages.Request().GetAsync().Result; any details want additionally? @ralf
    – Gokulsusan
    Commented Jul 1 at 11:58
  • That is presumably not relevant. currentPage is initialized in that line and when the graph call fails stays null. Check your call on the right side of that if something of that is null. How does the stacktrace looks?
    – Ralf
    Commented Jul 1 at 12:02
  • @ralf this is the inner exception stacktrace for the error--- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() Do you have any specific recommendations on what I should be looking for or any particular part of the code that might be causing this issue?
    – Gokulsusan
    Commented Jul 1 at 12:20
  • Sounds like GetAsync() fails and you simply can't access Result so you currently don't see the actual error because you try to read a result that isn't there and that comes in the way. You may want to use real "async/await" code instead of reading "Result" of a Task object.
    – Ralf
    Commented Jul 1 at 12:34

1 Answer 1

0

Default paging in the Graph is only 10 in email so if your trying to get all emails in a folder using that would be quite slow due to the number of requests you may need to make (if there are a large number of items in the folder) and your likelihood of getting throttled would be high also.

The Graph SDK has a PageIterator class so its best to use that, restrict the properties to just what you need and set the page size to up to 1000

eg

            var messages = graphServiceClient.Me.Messages
            .Request()
            .Select(e => new {
                e.Sender,
                e.Subject,
            })
            .Top(1000)
            .GetAsync().GetAwaiter().GetResult();

        var pageIterator = PageIterator<Message>
            .CreatePageIterator(
                graphServiceClient,
                messages,
                // Callback executed for each item in  
                // the collection  
                (m) =>
                {
                    Console.WriteLine(m.Subject);
                    return true;
                }
            );

        pageIterator.IterateAsync().GetAwaiter().GetResult();

You also might want to consider using the latest Kiota based version of the Graph SDK.

3
  • Hi @Glen, Thanks for the response. even i tried with your code even the error occurs-- at Microsoft.Graph.HttpProvider.<SendAsync>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__38.MoveNext()--- is that any code modification required?
    – Gokulsusan
    Commented Jul 2 at 4:46
  • No works as is, most likely its your auth code where is your token coming from ? if you using the client credentials flow then you would need to use User["[email protected]"].Messages Commented Jul 3 at 0:34
  • Hi @Glen yes, we're using client credentials and admin user for authentication. so, the code will same for this requirement?
    – Gokulsusan
    Commented Jul 3 at 5:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.