如何通过Microsoft Graph API获取邮件的收件人别名属性?
Great question! This is a super common scenario when working with Office 365 mail aliases—Exchange automatically resolves aliases to the primary mailbox in standard message properties like ToRecipients, which makes it tricky to find the original alias the email was sent to. Luckily, there’s a reliable workaround by digging into the message’s raw internet headers.
Step-by-Step Solution
1. Fetch the Message with Internet Headers
First, use the Graph API to retrieve the message and explicitly request the internetMessageHeaders property. This collection stores raw SMTP header data that preserves the original recipient alias before Exchange resolved it to the primary mailbox.
Example REST request:
GET https://graph.microsoft.com/v1.0/me/messages/{message-id}?$select=internetMessageHeaders
If you’re using the Graph SDK (C# example):
var graphClient = new GraphServiceClient(authProvider); var message = await graphClient.Me.Messages["{message-id}"] .Request() .Select("internetMessageHeaders") .GetAsync();
2. Extract the Original Recipient Alias
Next, scan the internetMessageHeaders collection for specific fields that hold the original alias. The most consistent ones are:
X-Original-To: Added by Exchange, this header contains the exact alias the email was sent to.OriginalRecipient: A fallback option for some environments whereX-Original-Toisn’t present.
Here’s how to extract the value in C#:
// Check for the X-Original-To header first var originalToHeader = message.InternetMessageHeaders .FirstOrDefault(h => h.Name.Equals("X-Original-To", StringComparison.OrdinalIgnoreCase)); if (originalToHeader != null) { string recipientAlias = originalToHeader.Value; Console.WriteLine($"Original recipient alias: {recipientAlias}"); } // Fallback to OriginalRecipient if needed else { var originalRecipientHeader = message.InternetMessageHeaders .FirstOrDefault(h => h.Name.Equals("OriginalRecipient", StringComparison.OrdinalIgnoreCase)); if (originalRecipientHeader != null) { string recipientAlias = originalRecipientHeader.Value; Console.WriteLine($"Original recipient alias: {recipientAlias}"); } }
3. Required Permissions
Ensure your app has the Mail.Read delegated or application permission (depending on your use case) to access message headers.
Why This Works
When an email is sent to an alias, Exchange routes it to the primary mailbox but retains the original recipient details in the SMTP headers. The ToRecipients property in the Graph API Message object returns the resolved primary mailbox address, but the raw headers keep the original alias intact.
内容的提问来源于stack exchange,提问作者MB1

