Change the Display Name of a Message sender when using Microsoft Graph

There are scenarios in which an application sends an email with the requirement that the sender name be something different than what is configured in Exchange. In an on-premises environment, this is straightforward. Setting the header in the message payload is usually sufficient because the authentication of the application by the mail server can happen outside of the application code (e.g. mail relay configuration, SMTP Auth, etc.).

When using Exchange Online via Microsoft Graph, things are quite different. The application must present a token (with or without user context). In addition, the sendEmail action requires a user or group id or user principal name as part of the request URL. When the message is sent, Exchange uses the display name from Azure AD in the From name.

There are a few cases in which the from property of the message can be changed. From the Graph API documentation for the message entity:

Setting the from and sender properties
When a message is being composed, in most cases, the From and Sender properties represent the same signed-in user, unless either is updated as described in the following scenarios:
  • The from property can be changed if the Exchange administrator has assigned sendAs rights of the mailbox to some other users. The administrator can do this by selecting Mailbox Permissions of the mailbox owner in the Azure Management Portal, or by using the Exchange Admin Center or a Windows PowerShell Add-ADPermission cmdlet. Then, you can programmatically set the from property to one of these users who have sendAs rights for that mailbox.
  • The sender property can be changed if the mailbox owner has delegated one or more users to be able to send messages from that mailbox. The mailbox owner can delegate in Outlook. When a delegate sends a message on behalf of the mailbox owner, the sender property is set to the delegate’s account, and the from property remains as the mailbox owner. Programmatically, you can set the sender property to a user who has got delegate right for that mailbox.

While those cases sound a bit compliated, the setup can be simplified -- use a shared mailbox. (For details on the creation of a shared mailbox, refer to this support article: Create a Shared Mailbox) When using the Exchange Admin Portal to create a shared mailbox, the members will get the sendAs rights for the mailbox. The application can then set the from property to the address of the shared mailbox.

As an example:

The following HTTP request will send a message with the from name equal to the display name of the shared mailbox. The sender is set based on the UPN in the URL ([email protected]) and the from property is set in the body.

POST https://graph.microsoft.com/v1.0/users/[email protected]/sendMail
Content-type: application/json

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ],
    "from": {
      "emailAddress": {
        "address": "[email protected]"
      }
    }
  }
}
comments powered by Disqus