Skip to content

Instantly share code, notes, and snippets.

@vman
Created April 12, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vman/0b2bdfc3b8f767b3499d8f122f288c36 to your computer and use it in GitHub Desktop.
Save vman/0b2bdfc3b8f767b3499d8f122f288c36 to your computer and use it in GitHub Desktop.
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
Copy link

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