2

In the Graph API explorer, you can send an email with the endpoint
https://graph.microsoft.com/v1.0/me/sendMail
and a basic json payload of

"message": {
    "subject": "Meet for lunch?",
    "body": {
        "contentType": "Text",
        "content": "The new cafeteria is open."
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "user@domain.com"
            }
        }
    ],
    "from":{
        "emailAddress": {
            "address": "smtp:my_alias@domain.com"
        }
    }
}


When I send this request, it still sends the email as user@domain.com instead of my email alias. I can still see the email alias there when i run the endpoint
https://graph.microsoft.com/beta/me/
Under proxyAddresses
I've looked over the documentation and don't see any clear example of option to send the email as alias.

Shiva Keshav Varma
  • 3,398
  • 2
  • 9
  • 13
Jensen
  • 199
  • 2
  • 11

2 Answers2

4

We can only set from and sender properties to a different value when sending a message from a shared mailbox, for a shared calendar, or as a delegate. See details here (see form and sender) and Setting the from and sender properties. Sending email as alias is not mentioned and cannot work based on the test.

So it's not supported to send the email as alias via Microsoft Graph API.

The value of from and sender must correspond to the actual mailbox used. So the only way to send email as alias is to change alias to primary email in O365 admin center and then send email.

As a workaround, if you just want recipients to think that you are sending from alias, you can configure a delegated mailbox for your mailbox (assign sendAs rights of the mailbox to a delegated user). Delegated mailbox is actually another mailbox and needs to be assigned an Exchange Online license. Then set the from property to the delegated user who have sendAs rights for your mailbox in Microsoft Graph API. See details here. After that, when you send an email, the recipient will see it's from the delegated email.

About how to assign sendAs rights in Exchange Admin Center, please refer to Use the EAC to assign permissions to individual mailboxes. It may take several hours to take effect.

POST https://graph.microsoft.com/beta/me/sendMail
{
    "message": {
        "subject": "Meet for lunch?",
        "body": {
            "contentType": "Text",
            "content": "The new cafeteria is open."
        },
        "toRecipients": [{
                "emailAddress": {
                    "address": "user@domain.com"
                }
            }
        ],
        "from": {
            "emailAddress": {
                "address": "{delegated mailbox}"
            }
        }
    }
}
Allen Wu
  • 15,529
  • 1
  • 9
  • 20
0

Just tested the API and verified working as of now.

https://developer.microsoft.com/en-us/graph/graph-explorer

POST https://graph.microsoft.com/beta/me/sendMail
{
  "message": {
    "subject": "Meet for lunch test?",
    "body": {
        "contentType": "Text",
        "content": "The new cafeteria is open."
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "somemail@gmail.com"
            }
        }
    ],
    "from": {
        "emailAddress": {
            "address": "myalias@contoso.com"
        }
    }
  }

}

"from" address must be from one of your proxy/alias email addresses.

If you're using AzureAD Tenant/Client IDs to send emails, you can use the sample code below:

var msg = new Message();
msg.Subject = "test";
//... other setup
msg.From = new Recipient { 
   EmailAddress = new EmailAddress 
               { Address = "myalias@contoso.com" } 
};
await graphClient.Users["mydefault@contoso.com"].SendMail(msg).Request().PostAsync();

I tried setting msg.Sender and it doesn't work.

KnaveT
  • 115
  • 1
  • 10
  • I tried this today and was unsuccessful. The request to Graph was successful, but the received e-mail used the primary e-mail, not the alias. – Halvor Holsten Strand May 02 '23 at 08:50
  • Just re-tested, using Nuget Microsoft.Graph, v4.54.0. Works fine. However, the latest version 5.8.0 breaks my code. – KnaveT May 03 '23 at 03:43
  • Testing this on GraphExplorer works as well. Do not send it to an email in your organisation. Send it externally, e.g. Gmail/Outlook. – KnaveT May 03 '23 at 03:55
  • Hmm. YMMW I guess. Can't make it work. Sending from my business email with `from` as my alias, but the raw MIME in Gmail still displays my primary email address as the `from` header (and display value in the GUI). Strange. This was attempted from Graph Explorer. – Halvor Holsten Strand May 03 '23 at 07:30