Get attachments for a particular Email

Kasim Sharif 1 Reputation point
2023-01-09T06:24:37.843+00:00

I am using Microsoft Graph API to get attachments of particular email.

https://graph.microsoft.com/v1.0/users/<user-id>/messages/<attachments>/attachments
Above API returns attachments from previous conversation thread as well.

Suppose a mail was started and it had bunch of replies with attachments and my latest email contains only one attachment and in entire email there are like 5 attachments with latest message-id if i try to fetch attachment instead of returning 1 attachment, it returns all attachment.

Is there any filter available just to get that particular attachment of the given mail.

i tried to get attachments of that particular sent mail but instead i am getting attachments of entire thread

Microsoft Security | Microsoft Graph
{count} votes

2 answers

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,446 Reputation points
    2023-01-10T06:23:32.637+00:00

    Hi @Kasim Sharif ,

    Thanks for reaching out.

    I am unable to reproduce your issue from my test tenant. As per the details you are passing the incorrect Graph API endpoint. Can you please execute the MS Graph API: GET /users/{User-id}/messages/{message-id}/attachments. This graph call will return only the attachment details of that particular message in response based on message-id.

    In my below example I have a mail conversation with multiple attachments of total 4, but when I query for latest message i got only 2 attachment details in response.

    277756-attach-1.png

    277750-attache-2.png

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.


  2. CarlZhao-MSFT 46,376 Reputation points
    2023-01-10T08:09:43.9+00:00

    Hi @Kasim Sharif

    Welcome to the Q&A Forum!

    First of all, I am not sure if it is your writing error, /users/{user-id}/messages/{attachments}/attachments is not a valid api endpoint, the correct endpoint should be: /users/{user-id}/messages/{message-id}/attachments.

    However, even with the correct endpoint, you cannot list specific attachments under a specific message, which returns only one collection of attachment lists. If you want to get a specific attachment under a specific message, then it may take multiple calls, because the endpoint does not appear to be able to filter using the $filter parameter.

    1. /users/<user-id>/messages/<message-id>/attachments
    2. /users/<user-id>/messages/<message-id>/attachments/<attachment-id>
    

    If you are interested in automation, then using the graph C# SDK is undoubtedly a good solution, you just need to traverse the attachment set and then make a simple judgment with a specific attachment name to get a specific attachment under a specific message:

    using Azure.Identity; 
    using Microsoft.Graph;
    using Newtonsoft.Json;
    
    try
    {
    
    var scopes = new[] { &#34;https://graph.microsoft.com/.default&#34; };
    
    var tenantId = &#34;{tenant id}&#34;;
    
    // Values from app registration
    var clientId = &#34;{client id}&#34;;
    var clientSecret = &#34;{client secret}&#34;;
    
    // using Azure.Identity;
    var options = new TokenCredentialOptions
    {
          AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
          tenantId, clientId, clientSecret, options);
    
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
    var attachments = await graphClient.Users[&#34;{user name}&#34;].Messages[&#34;message id&#34;].Attachments.Request().GetAsync();
    
          for (int i = 0; i &lt; attachments.Count; i++) {
    
                var attachmentName = attachments[i].Name;
    
                if (attachmentName == &#34;{attachment name}&#34;) {
    
                      Console.WriteLine(&#34;att:&#34; + JsonConvert.SerializeObject(attachments[i]));  
                }          
          }
    }
    catch (Exception ex) { 
    
          Console.WriteLine(ex);
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.