Showing posts with label Send Email. Show all posts
Showing posts with label Send Email. Show all posts

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.

February 16, 2017

Nintex Workflow: Access denied. You do not have permission to perform this action or access this resource.

Working with Nintex workflow, I was trying to send an email through "Send Email" action to users within SharePoint Group. If user through which workflow is kicked off, is part of that SharePoint Group then workflow/email works fine. But If user is not added in that group, getting below error: (Even that user has full control permission on site.)

"Access denied. You do not have permission to perform this action or access this resource."

Cause:
Basically, when "Send Email" action executes, Nintex try to get users information to whom email needs to be sent out. If logged in user is added in the SharePoint group, Nintex is able to retrieve the information. But if user is not part of the group, it won't be able to access member information of the group.

Resolution:
To overcome this situation, Allow everyone to see the members of the SharePoint group. To make this change, follow below steps:


Note: This will enable other users who are not part of the group to view all  members of  the group.

  • Go Site Settings --> People & Groups, browse to that SharePoint Group.

  • From settings menu, select Group Settings.
  • Update Group settings --> view permission to "Everyone".
  • Click OK.

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