Convert nested mail to "Microsoft.Graph.Models.Message" using Microsoft Graph .NET Client Library 5.x

Christian Luif 40 Reputation points
2023-09-17T13:59:07.67+00:00

Hi Microsoft-Team,

following question: I have an E-Mail with a nested E-Mail inside (attachment). I download the attachment(the nested mail) and want to bring it back into a "Microsoft.Graph.Models.Message"-class so I can iterate over it.

Is this possible?

Many thanks in advance &

Regards

Christian

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,019 questions
0 comments No comments
{count} votes

Accepted answer
  1. Glen Scales 4,436 Reputation points
    2023-09-18T00:42:07.0033333+00:00

    I download the attachment(the nested mail) and want to bring it back into a "Microsoft.Graph.Models.Message"-class so I can iterate over it.

    What format is the message you downloaded in eg did you use $value and get the MimeContent of the Message, if so there is no way of converting it directly back into the Message class without uploading to the server (eg create a draft and then access back the draft). If you have an EML/Mime message and you need to iterated on that message then you would be better of using a MIME library like Mime/MaiKit http://www.mimekit.net/ . If you have the ItemAttachment in it Native Json format (or you just saved the Message of and left it in Json it pretty easy to deserialize it back with the Kiota SDK's eg

                string itemAttachmentjsonMessage = @"{
      ""@odata.context"":""https://graph.microsoft.com/v1.0/$metadata#users('d1a2fae9-db66-4cc9-8133-2184c77af1b8')/messages('AAMkADA1M-zAAA%3D')/attachments/$entity"",
      ""@odata.type"":""#microsoft.graph.itemAttachment"",
      ""id"":""AAMkADA1MCJKtzmnlcqVgqI="",
      ""lastModifiedDateTime"":""2017-07-21T00:20:34Z"",
      ""name"":""Reminder - please bring laptop"",
      ""contentType"":null,
      ""size"":32005,
      ""isInline"":false,
      ""item"":{
        ""@odata.type"":""#microsoft.graph.message"",
        ""id"":"""",
        ""createdDateTime"":""2017-07-21T00:20:41Z"",
        ""lastModifiedDateTime"":""2017-07-21T00:20:34Z"",
        ""receivedDateTime"":""2017-07-21T00:19:55Z"",
        ""sentDateTime"":""2017-07-21T00:19:52Z"",
        ""hasAttachments"":false,
        ""internetMessageId"":""<BY2PR15MB05189A084C01F466709E414F9CA40@BY2PR15MB0518.namprd15.prod.outlook.com>"",
        ""subject"":""Reminder - please bring laptop"",
        ""body"":{
          ""contentType"":""html"",
          ""content"":""<html><head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>""
        },
        ""sender"":{
          ""emailAddress"":{
            ""name"":""Adele Vance"",
            ""address"":""AdeleV@contoso.onmicrosoft.com""
          }
        },
        ""from"":{
          ""emailAddress"":{
            ""name"":""Adele Vance"",
            ""address"":""AdeleV@contoso.onmicrosoft.com""
          }
        },
        ""toRecipients"":[
          {
            ""emailAddress"":{
              ""name"":""Alex Wilbur"",
              ""address"":""AlexW@contoso.onmicrosoft.com""
            }
          }
        ],
        ""ccRecipients"":[
          {
            ""emailAddress"":{
              ""name"":""Adele Vance"",
              ""address"":""AdeleV@contoso.onmicrosoft.com""
            }
          }
        ],
        ""flag"":{
          ""flagStatus"":""notFlagged""
        }
      }
    }";
    
    
                //Desearlize it back
                var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(itemAttachmentjsonMessage));
                var parseNodeFactory = new JsonParseNodeFactory();
                var parseNode = parseNodeFactory.GetRootParseNode(CoreConstants.MimeTypeNames.Application.Json, memoryStream);
                var parsedAttachment = parseNode.GetObjectValue<ItemAttachment>(ItemAttachment.CreateFromDiscriminatorValue);
                if(parsedAttachment.Item is Message)
                {
                    Console.WriteLine(((Message)parsedAttachment.Item).Subject);
                }
               
                ```
    
    

0 additional answers

Sort by: Most helpful

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.