Today we are going to talk about sending emails via Microsoft Graph API from Salesforce. The reason is that one of our clients experienced a necessity in sending emails via Microsoft Outlook.
Salesforce has a solution for redirecting messages to the SMTP-server using “Email Relay Activation”. With this function, you can redirect all messages to the client server. But this solution is not suitable, since the redirection of letters will be applied to all letters that will be sent from salesforce. Therefore, it was decided to find another solution and it became the Microsoft Graph API. Using this API, we can manipulate your account information and interact with other microsoft accounts and products. 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 suitable, as it is necessary for the user to participate in the authorization interface. Since we need to send letters in background processes, the second method is right what you need. In order to use this method, it is necessary to perform a lot of actions, namely:
- Create an application in the “Microsoft App Registration Portal”
- Give permission to send email to your application
- Obtain the consent of the administrator
Eventually, we can use these settings to get a token. 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 method of sending emails and for this we will use the “Send mail” method. This method is simple, you need to create a “Message” object, which includes all the information about the letter and make a POST request to a specific endpoint. Full information about the method can be found in this documentation.
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. Also this solution 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 sending a letter. Specifically, in one context it is possible to send no more than 99 emails, which is also not bad.
Hope this article was very useful and informative. 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 on firstname.lastname@example.org
This article was originally published here by Vladimir Zhutenko.
Editor: Anna Krivulya