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?

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>{"a@mail.com"};
    emailp.To = new List<string>{"b@mail.com"};
    emailp= "from@mail.com";
    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.

| improve this answer | |
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?

| improve this answer | |
  • i am not using a console app i am using a remote event reciever – SharePoint TestDev May 12 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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