Created
April 12, 2017 09:55
-
-
Save vman/0b2bdfc3b8f767b3499d8f122f288c36 to your computer and use it in GitHub Desktop.
Send email using CSOM to authenticated external users in SharePoint Online
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.?