DEV Community

Gokulsusan
Gokulsusan

Posted on

Unable to get the emails from outlook using this method (using graph api)

/// <summary>
/// Initializes a new instance of <see cref="Office365MailServiceClient"/>.
/// </summary>
/// <param name="connectionParams">Connection string as a key value pair.</param>
public Office365MailServiceClient(Dictionary<string, string> connectionParams)
{
    this.graphAuthenticatorV1 = new MSGraphAuthenticator(connectionParams, MSGraphAuthenticator.GraphAPIVersion.v1);
    this.graphClientV1 = this.graphAuthenticatorV1.GetAuthenticatedClient();
}

      private GraphServiceClient graphClientV1;

/// <summary>
/// Gets the mail folder name by id.
/// </summary>
/// <param name="parentFolderId">Parent Folder Id.</param>
/// <returns></returns>
public string GetMailFolderNameById(string parentFolderId)
 => graphClientV1.Me.MailFolders[parentFolderId].Request().GetAsync().Result?.DisplayName ?? string.Empty;


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;
}

getting this error:

   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)


can anyone help me to get resolve this method?
-Thanks in advance.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)