Before having look at the code, I should mention my site collection has external sharing turned on and the "Allow external users who accept sharing invitations and sign in as authenticated users" option selected.
And here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Thanks for reading!
No comments:
Post a Comment