Charlie
Charlie

Reputation: 11

Need help how to retrieve attachments from a message using graph sdk 5.5

I have used c# code with graph sdk 5.5 to access office 365 mailbox. I can read all the messages via my code, but cannot retrieve any attachments. Cannot found any working example that works for SDK 5.5.

I have found below snippet but I do not know where to get the "attachment id" :

var requestInfo = graphClient.Users["{user_id}"]
                             .Messages["{message_id}"]
                             .Attachments["{attachment_id}"]
                             .ToGetRequestInformation();

Can anyone help me with this issue?

Here is my current code:

var credentials = new ClientSecretCredential(directoryTenantID, applicationClientID, secretID);
 var graphServiceClient = new GraphServiceClient(credentials);

 // Read emails from the authenticated user's mailbox
 try
 {
     Microsoft.Graph.Models.MessageCollectionResponse messageCollectionResponse = await graphServiceClient
         .Users["***********"]
         .MailFolders["Inbox"]
         .Messages
         .GetAsync(config => config.QueryParameters.Top = 50);

     var messages = messag`your text`eCollectionResponse.Value;

     foreach (var message in messages)
     {
         var attachments = graphClient.Users["[email protected]"].Messages[message.Id].Attachments.GetAsync();   (get an error on this line)                
         foreach (var attachment in attachments)
         {
             // Save attachment to a specific folder on your hard disk
             // Customize the folder structure based on your needs
             // For example:
             // attachment.Save("C:\\Attachments\\" + attachment.Name);
         }
     }
}

Upvotes: 1

Views: 263

Answers (1)

Christian
Christian

Reputation: 5541

When you call an asynchronous method, use the await keyword to ensure that the execution waits for the operation to complete. See graphServiceClient for attachments below.

var credentials = new ClientSecretCredential(directoryTenantID, applicationClientID, secretID);
var graphServiceClient = new GraphServiceClient(credentials);

 // Read emails from the authenticated user's mailbox
 try
 {
     Microsoft.Graph.Models.MessageCollectionResponse messageCollectionResponse = await graphServiceClient
         .Users["***********"]
         .MailFolders["Inbox"]
         .Messages
         .GetAsync(config => config.QueryParameters.Top = 50);

     foreach (var message in messages)
     {
         var attachments = await graphServiceClient.Users["***********"]
                                                         .Messages[message.Id]
                                                         .Attachments
                                                         .GetAsync();   

         foreach (var attachment in attachments)
         {

             // For example:
             System.IO.File.WriteAllBytes($"C:\\Attachments\\{fileAttachment.Name}", fileAttachment.ContentBytes);
             Console.WriteLine($"Saved {fileAttachment.Name}");
         }
     }
}

Upvotes: 0

Related Questions