May 20, 2021

How to send email without username and password using SMTP in C# in Office 365

Introduction:

We implemented a Console Application (C#) for a Real Estate Agency based out of Bellevue, Washington, United States. It was a part of the requirement to send an email from the Console Application using SMTP. Generally, we need the password of from email account when we send mail using SMTP, but in our case, we have to use an email account that has Okta MFA enabled.So, if we use the password for authentication, it won't work. As an alternate, we implemented the solution to send an email from C# Console Application (via SMTP) using App Only Authentication (with Azure App Registration).

Implementation:

First, we need to setup Azure App:

Setup Azure App:

Step 1: Go to https://portal.azure.com/.

Step 2: Now click on Azure Active Directory.  


Step 3: Now Click on "App Registration" from the left panel.


Step 4: Now Click on "New registration".


Step 5: Type the name of the app (a meaningful name) and click on the "Register" button.



Step 6: Now click on "App Permission" in left panel and add below API permissions:

  1. API Permission  > Microsoft Graph > Delegated Permission > User.Read
  2. API Permission > Add a permission > APIs in my organization uses > Office 365 Exchange Online > Application Permission > Other permission > full_access_as_app


Step 7: To use this permission of app, we need to get admin consent by Office 365 Global Admin user.


Step 8: Now go to "Certificates and secrets", and create a new client secret. Once this secret key created, note this secret key.


Step 9: Now click on "Overview" in the left panel to find Application (client) ID and Directory (tenant) ID.



C# Code

Step 1: Now go to your visual studio solution and add nuget package for EASendMail.


Step 2: Add below namespace in code.

 using System;  
 using EASendMail;  


Step 3: Now, use the below code to send mail:

 try  
 {  
     string client_id = "*****-****-****-****-*******";  
     string client_secret = "**************";     
     string tenant_id = "********-****-****-****-*******";   
     string requestData = string.Format("client_id={0}&client_secret={1}&scope=https://outlook.office365.com/.default&grant_type=client_credentials",  
     client_id, client_secret);  
     string tokenUri = string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenant_id);  
     string responseText = _postString(tokenUri, requestData);  
     OAuthResponseParser parser = new OAuthResponseParser();  
     parser.Load(responseText);  
     string officeUser = fromEmailAddress;  
     var server = new SmtpServer("outlook.office365.com");  
     server.Protocol = ServerProtocol.ExchangeEWS;  
     server.User = officeUser;  
     server.Password = parser.AccessToken;  
     server.AuthType = SmtpAuthType.XOAUTH2;  
     server.ConnectType = SmtpConnectType.ConnectSSLAuto;  
     var mail = new SmtpMail("TryIt");  
     mail.From = officeUser;  
     mail.To = toEmailAddress;  
     mail.Subject = "Office 365 background service oauth test";  
     mail.HtmlBody = "<h3>Hello</h3><p>Do not reply to this mail.</p>";  
     var smtp = new SmtpClient();  
     smtp.SendMail(server, mail);   
     Console.WriteLine("Message delivered!");  
}  
catch (Exception ex)  
{  
     Console.WriteLine(ex.ToString());  
}  


Step 4: In above code, you can see we have not used the password of the user which is selected as mail from.

Conclusion:

This is how we can send email from C# Console Application without using the password of from email address. Hope this helps!

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment