Home > front end >  Can we get the from/sender and Recipient emails from graph API response?
Can we get the from/sender and Recipient emails from graph API response?

Time:04-18

I'm trying to forward an email if the attachment is not available. I'm able to get the code working up to moving the email to another folder but I can't understand how to get the email addresses.

Below is the code I've written.

if (ErrorFolder.DisplayName == "Error")
{
  var MessageID2 = Message.Id;
  var ErrorFolderId = ErrorFolder.Id;
  
 var MsgDetails = await graphClient.Me.MailFolders["Inbox"]
   .ChildFolders[$"{FirstSubFolderID.Id}"]
   .ChildFolders[$"{SecondSubFolderID.Id}"]
   .ChildFolders[$"{ErrorFolderId}"]
   .Messages[$"{MessageID2}"]
   .Request()
   .Select(em => new
   {
     em.Subject,
     em.From
   })
   .GetAsync();
 //var msg = MsgDetails.From;

 Console.WriteLine("Email Subject: {0}", MsgDetails.Subject);
 Console.WriteLine("From Email: {0}", MsgDetails.From);
}

The from property returns the values like below (according to graph documents).

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('ee4376d7-d243-47a9-9729-dd398d0d1e5a')/mailFolders('inbox')/childFolders('AQMkADRlMDQwMjliLWU5NjItNDE5My05YjQ3LWJlNGRjYwBiZDM0MGEALgAAA2sRhR3zhaNNonFGlS_1NfIBAKdBNt_yapZOqPO2vlH2EfEAAAJOOgAAAA==')/childFolders('AAMkADRlMDQwMjliLWU5NjItNDE5My05YjQ3LWJlNGRjY2JkMzQwYQAuAAAAAABrEYUd84WjTaJxRpUvtTXyAQCnQTbfsmqWTqjztr5R9hHxAAAGdVmSAAA=')/childFolders('AAMkADRlMDQwMjliLWU5NjItNDE5My05YjQ3LWJlNGRjY2JkMzQwYQAuAAAAAABrEYUd84WjTaJxRpUvtTXyAQCnQTbfsmqWTqjztr5R9hHxAAAdYEb3AAA=')/messages(from)/$entity",
    "@odata.etag": "W/\"CQAAABYAAACnQTbfsmqWTqjztr5R9hHxAAAdWOKS\"",
    "id": "AAMkADRlMDQwMjliLWU5NjItNDE5My05YjQ3LWJlNGRjY2JkMzQwYQBGAAAAAABrEYUd84WjTaJxRpUvtTXyBwCnQTbfsmqWTqjztr5R9hHxAAAdYEb3AACnQTbfsmqWTqjztr5R9hHxAAAdYGJwAAA=",
    "from": {
        "emailAddress": {
            "name": "Sandaruwan Samaraweera",
            "address": "[email protected]"
        }
    }
}

I want to get the 2 values under from and then use those when I forward the email back to the sender. Can this be done?

CodePudding user response:

You can try this and it worked in my side:

var a = await graphClient.Me.MailFolders["Inbox"]
                .Messages["message_id"]
                .Request()
                .Select(em => new
                {
                    em.Subject,
                    em.From
                })
                .GetAsync();
var b = a.From.EmailAddress.Name;
var c = a.From.EmailAddress.Address;

enter image description here

  • Related