Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send email using CSOM to authenticated external users in SharePoint Online
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;
using System;
using System.Security;
namespace ExternalUsersEmail
{
class Program
{
static void Main(string[] args)
{
//Authenticated ClientContext. I am using SharePointOnlineCredentials but YMMV.
var clientContext = GetClientContext();
var web = clientContext.Web;
//Full logon name of the authenticated external user you want to send the email to.
web.EnsureUser("i:0#.f|membership|paige.turner_contoso.com#ext#@tenant.onmicrosoft.com");
clientContext.ExecuteQuery();
var emailProperties = new EmailProperties();
//Email of authenticated external user
emailProperties.To = new string[] { "paige.turner@contoso.com" };
emailProperties.Body = "This is the email body";
emailProperties.Subject = "This is the email subject";
Utility.SendEmail(clientContext, emailProperties);
clientContext.ExecuteQuery();
}
private static ClientContext GetClientContext()
{
string siteUrl = "https://tenant.sharepoint.com/sites/comms/";
var clientContext = new ClientContext(siteUrl);
string passwordPlainText = "userpassword";
string currentUserLoginName = "user@tenant.onmicrosoft.com";
var passWord = new SecureString();
foreach (char c in passwordPlainText.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(currentUserLoginName, passWord);
return clientContext;
}
}
}
@sandippal972258

This comment has been minimized.

Copy link

@sandippal972258 sandippal972258 commented Jan 18, 2020

Its not working in my case, I can send email from outlook but Programmatically , i can't send EMAIL. Is there any specific licence needed for send email.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment