How to Send Emails via Outlook API from your Salesforce Org
Hi there. This is Salesforce Developer Vladimir Zhutenko. Recently, one of our clients experienced a necessity in sending emails via Microsoft Outlook from his SF org. So, I’ve come up with a solution, which I will describe below.
How to use Microsoft Graph API
Salesforce has a solution for redirecting messages to the SMTP-server using Email Relay Activation. With this function, you can turn all messages to the client-server. But this option isn’t suitable since the redirection of letters will be applied to all letters sent from Salesforce.
Therefore, it was decided to find another solution, and it became the Microsoft Graph API. We can manipulate your account information and interact with other Microsoft accounts and products using this API. But, most importantly, it solves our problem, sending letters via Outlook.
The first step is to understand authentication in this system. The documentation offers two ways to get the token: “Get access on behalf of the user” and “Access without user participation.”
The first method is not fitting, as the user must participate in the authorization interface. Since we need to send letters in background processes, the second method is suitable for what you need. To use this method, it’s essential to perform a lot of actions, namely:
- Create an application in the “Microsoft App Registration Portal”
- Permit to send an email to your application
- Obtain the consent of the administrator
Eventually, we can use these settings to get a token. But, again, more details you may find in the documentation.
Our application is configured for sending emails. A method for authorizing and receiving a token has been created. Next, we need a way of sending emails, and for this, we will use the Send mail method. This process is simple.
You need to create a Message object, which includes all the information about the letter and makes a POST request to a specific endpoint.
In the end, we have a solution that solves the problem of sending emails through Outlook. This solution is suitable for the corporate segment, where it is necessary to send emails within the organization.
However, this resolution has its drawbacks in the form of limits on the number of API calls in one context. So for sending one letter, you need to make two API calls: the first is to log in and get a token. The second is to send a letter. Specifically, it is possible to send no more than 99 emails in one context, which is also not bad.
Hope this article was helpful. For more information about our development, you may explore our website. If you have any issues or questions, please, do not hesitate to send us emails at email@example.com.