In the past azure (outlook 365) let users use SMTP to send email from apps, but in the past year this has changed and tokens granted by Micrsoft.Client.Identity cannot be used in the MailKit SmtpClient unless the users outlook365 account removes default azure security. I am not sure why they did this but we have been looking for a way to send emails through our apps.
We found this question and article on sending email using Microsoft graph, and this works. In the article the code sets up a json email message. I want to know if we can use MailKit to set up an email message and send using a MailKit method, or would we just need to serialize email created using MailKit into json and then do the PostAsync as the article does.
Yes, you can make use of Malkit to send mail like below:
Create an Azure AD application and assign below API permissions:
Grant access to the user:
For sample, I used client credential flow to generate the token by using below parameters:
By using the above token you call send mail like below:
But it is suggested to make use of Microsoft Graph API to send mail for sing OAuth2 authentication. Refer this SO Thread by me
References:
Authenticate an IMAP, POP or SMTP connection using OAuth | Microsoft
MailKit/ExchangeOAuth2.md at master · jstedfast/MailKit · GitHub by jstedfast