0

I have a server-side event receiver inside our SharePoint 2013 farm, and inside the ER i am sending emails using this code:-

 using (MailMessage myMailMessage = new MailMessage())
            {


                SPOutboundMailServiceInstance smtpServer = mailserverinstance;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer.Server.Address);


                myMailMessage.IsBodyHtml = true;
                myMailMessage.Subject = title;

                myMailMessage.Body = emailbody;

                myMailMessage.From = new MailAddress(fromField);
                string[] emailaddresses = to.Split(';');
                foreach (var toemail in emailaddresses)
                {
                    if (!string.IsNullOrEmpty(toemail))
                    {
                        myMailMessage.To.Add(new MailAddress(toemail));
                    }

                }
                if (!string.IsNullOrEmpty(cc))
                    myMailMessage.CC.Add(new MailAddress(cc));
                if (myMailMessage.To.Count > 0)
                {
                    smtp.Send(myMailMessage);
                }

            }

now i want to re-code my server-side event receiver, to work inside our SharePoint online tenant, so i am not sure how i will be sending emails inside my remote event receiver using CSOM? any sample codes?

2 Answers 2

1

This has been the most famous method for a sending email. You can use SharePoint Utility class to send an email. But you cannot send to external users. If you are sending to external users, they should be added to your mail exchange, but it will take some time to reflect the change.

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
    var emailp = new EmailProperties();
    emailp.BCC = new List<string>{"[email protected]"};
    emailp.To = new List<string>{"[email protected]"};
    emailp= "[email protected]";
    emailp.Body = "<b>html</b>";
    emailp.Subject = "subject";

    Utility.SendEmail(_clientContext, properties);
    _clientContext.ExecuteQuery();
}

You can use the same method with remote event receivers only by changing SharePoint client context initiation.

4
1

Yes you can use CSOM to send emails in SPO. @Karthik Jaganathan has provided a good demo.

As for the context, for example, if you're developing a console app, then you may need to pass credential to generate client context like below:

SendEmailExternalUser.cs

While for a remote event receive, we can get the context without credential: enter image description here

For more details, See below official docs and samples:

How does remote event receiver get authorized with SharePoint?

1
  • i am not using a console app i am using a remote event reciever
    – John John
    Commented May 12, 2020 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.