none
Unable to read attachments using Outlook Rest API RRS feed

  • Question

  • I am trying to read an email attachment using Outlook Rest API but I am getting "Object reference not set to an instance" error

    I have verified the app permissions in Azure Active Directory and has all the permissions set mentioned in microsoft documentation Mail.Read Group.Read.All (Added extra permission to try)

    I am able to read the message and its content correctly and even the message "HasAttachments" property is true but when I call API to fetch attachments it gives error.

    Also tried to create new token for fetching attachments considering the fact that existing one would have expired but no luck

    Following is my sample code:

    //Used to create access token 

    public async Task<string> GetAccessToken()
            {
           string accessToken = null;
               using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(TokenUri);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    var requestContent = string.Format("grant_type=password" + "&client_id=" + appId +
                                                        "&client_secret=" + appPassword +
                                                        "&resource=" + Resource +
                                                        "&username=" + userEmail +
                                                       "&password=" + passwd);
                    var content = new StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded");
                    var response = client.PostAsync(client.BaseAddress, content).Result;
                    var result = response.Content.ReadAsStringAsync().Result;
                    var jobject = JsonConvert.DeserializeObject<JObject>(result);
                    accessToken = jobject.GetValue("access_token").ToString();
                }
                return accessToken;
            }

    //Created a client using access token from above function and tried to get attachments as follows

    OutlookServicesClient client = new OutlookServicesClient(new Uri(OutlookAPI), GetAccessToken);

    client.Context.SendingRequest2 +=
                        new EventHandler<Microsoft.OData.Client.SendingRequest2EventArgs>((sender, e) => InsertXAnchorMailboxHeader(sender, e, userEmail));

    //The below line of code of reading message works perfectly

    var message = await client.Me.MailFolders.GetById(constInbox).Messages.GetById(strMessageID).ExecuteAsync();

    //But when I use the same thing for attachments it gives object reference error

     var attachments = await client.Me.MailFolders.GetById(constInbox).Messages.GetById(strMessageID).Attachments.ExecuteAsync();

    Kindly let me know the correct approach of reading the attachments from the email using Outlook Rest API.








    Thursday, September 19, 2019 1:12 PM