problem with loading email content via graph api

vujtas 1 Reputation point
2022-08-16T16:47:31.187+00:00

Hello, I will send an email using the method:

var response = _service.Users[from].SendMail(  
                 new Message  
             {  
                 Subject =subject,  
                 Body = new ItemBody   
                     {   
                     ContentType = Microsoft.Graph.BodyType.Html,   
                     Content = message   
                 },  
                 ToRecipients = recipient,  
                 CcRecipients=ccRecipient,  
                 Attachments=attachments,  
                 SingleValueExtendedProperties=new MessageSingleValueExtendedPropertiesCollectionPage()  
                 {  
                     new SingleValueLegacyExtendedProperty()  
                     {  
                         Id= $@"String {{630b1eb6-293a-4c92-ba78-a40aadad5ae7}} Name SendMailId",  
                         Value="nothing"  
                     }  
                 }  
             }, false).Request().PostResponseAsync().GetAwaiter().GetResult();  

method and then I will return the emails that contain the key via this method:

            var c = _service.Users[from].Messages.Request(new List<Option>()  
            {  
                new QueryOption("filter","singleValueExtendedProperties/Any(ep:ep/id eq 'String {630b1eb6-293a-4c92-ba78-a40aadad5ae7} Name SendMailId' and ep/value eq 'nothing')"),  
            }  
                    ).GetAsync().GetAwaiter().GetResult();  

I will retrieve the id of the email from this method and then I want to return the contents of the email using this method like this

var stream = _service.Users[from].Messages[c[0].Id].Content.Request().GetAsync().GetAwaiter().GetResult();  

Unfortunately, this solution does not work, or rather very rarely works and rather does not work.

My goal is to get the contents of the email after sending the email.

Thank you

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,515 questions
{count} votes

1 answer

Sort by: Most helpful
  1. vujtas 1 Reputation point
    2022-08-17T13:16:20.977+00:00

    Hello,
    my colleague finally found what the problem was. The parameter when sending an email to save to the sent folder must be "true". Also, when setting a custom parameter, you need to leave a constant value as the key, but enter a unique value in the "value" value. We then search using this key and value.
    so the right solution is:

                        var emailMessage = new Microsoft.Graph.Message()  
                        {  
                            Subject = subject,  
                            ToRecipients = to,  
                            Body = new Microsoft.Graph.ItemBody()  
                            {  
                                ContentType = Microsoft.Graph.BodyType.Html,  
                                Content = message  
                            },  
                            CcRecipients = cc,  
                            SingleValueExtendedProperties=new MessageSingleValueExtendedPropertiesCollectionPage()  
                            {  
                               new  Microsoft.Graph.SingleValueLegacyExtendedProperty()  
                               {  
                                   Id = $@"String {{630b1eb6-293a-4c92-ba78-a40aadad5ae7}} Name EmailSender",  
                                   Value = uniqueVal  
                               }  
                            }  
      
     var c = _service.Users[from].Messages.Request(new List<Option>()  
                            {  
                                new QueryOption("filter",  
                                @"singleValueExtendedProperties/Any(ep:ep/id eq 'String {630b1eb6-293a-4c92-ba78-a40aadad5ae7} Name EmailSender' and   
                                 ep/value eq '"+uniqueVal+"')"),  
                            }  
                                    ).GetAsync().GetAwaiter().GetResult();  
      
      
    var emailResponse = _service.Users[from].SendMail(emailMessage, true)  
                            .Request().PostResponseAsync().GetAwaiter().GetResult();  
      
    Stream stream = _service.Users[from].Messages[emailId].Content.Request().GetAsync().GetAwaiter().GetResult();  
      
      
      
    
    0 comments No comments