Search code examples
c#microsoft-graph-api.net-6.0

MSGraph/Messages returns ldap-path? instead of actual address


So when parsing some emails I encountered this problem on emails sent to myself:

 var email_ids = await Graph
                .Users[user.Id]
                .MailFolders[SrcFolderId]
                .Messages.Request()
                .Select(x=> new { Id=x.Id,Subject = x.Subject,From = x.From,Received = x.ReceivedDateTime,Sender = x.Sender})
                .GetAsync();

                foreach (var msg in email_ids)
                {
                   var from = msg.From.EmailAddress.Address; 
                   //returns:"/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=A744B91971114B01B89FD0C4B28F412F-JANECKI MAR"
                } 

On other emails the attribute works. bug or feature?


Solution

  • Exchange always stores email addresses in it's Native EX format and the Graph will generally resolve them (from the the directory eg AAD) when you make the query. However if the user has been deleted (or the mailbox has been migrated and the address is no longer valid) it can't resolve them anymore and will just return the native address. There are also some edge cases where this happens when you enumerate messages in those cases just doing a Get on the email id endpoint (/messsage/{id}) should work (but is really expensive in terms of throttling).

    What i would suggest is if your always need the SMTP address is include https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagsendersmtpaddress-canonical-property in your query in the case you get a EX address returned then you will always have the SMTPaddress available without needing to make any extra queries. eg

               .Users[user.Id]
               .MailFolders[SrcFolderId]
               .Messages.Request()
               .Expand("singleValueExtendedProperties($filter=id eq 'String 0x5D01')")
               .Select(x => new { Id = x.Id, Subject = x.Subject, From = x.From, Received = x.ReceivedDateTime, Sender = x.Sender, SingleValueExtendedProperties = x.SingleValueExtendedProperties})
               .GetAsync();
    

    in v5 something like

            string selectList = "Id,Subject,From,ReceivedDateTime,Sender,SingleValueExtendedProperties";
            string exProp = "singleValueExtendedProperties($filter=id eq 'String 0x5D01')";
            var messages = graphServiceClient.Users[userId].MailFolders["inbox"].Messages.GetAsync(requestConfiguration =>
            {
                requestConfiguration.QueryParameters.Select = new string[] { selectList };
                requestConfiguration.QueryParameters.Expand = new string[] { exProp };
            }).GetAwaiter().GetResult().Value.Select(x => new { Id = x.Id, Subject = x.Subject, From = x.From, Received = x.ReceivedDateTime, Sender = x.Sender, SingleValueExtendedProperties = x.SingleValueExtendedProperties });
            foreach(var message in messages)
            {
                Console.WriteLine(message.Sender);
            }