0

I have the following code inside my CSOM console application to send email:

static private void sendemail(ClientContext context, string subject, string body, string email)
       {
            var emailp = new EmailProperties();
            emailp.Subject = subject;
            emailp.Body = body;
            List<string> toUsers = new List<string>();
            toUsers.Add(email);
            emailp.To = toUsers;

            Utility.SendEmail(context, emailp);
            context.ExecuteQuery();
        }

Now the email will be sent correctly incase the to email is defined inside office 365, otherwise no email will be sent.

Any idea why?

1

The default SharePoint Utility.SendMail functionality in SharePoint is limited. And you can only send emails to people who are known within the site collection.

Source: Fail to send email by utility class

However you can send emails in SharePoint Online to external users who have accepted sharing invitations and signed in as authenticated users.

And for that your site collection should have external sharing turned on and the "Allow external users who accept sharing invitations and sign in as authenticated users" option selected.

enter image description here

Source: Send emails to authenticated external users using CSOM

| improve this answer | |
  • @Genesh Sanap in our case we do not allow external sharing for the site, so is there a way to send email through CSOM code to users not defined inside the site collection? – SharePoint TestDev May 19 at 10:22
  • I am not entirely sure but check the first reference link in my answer. @Atish Dipongkor - MVP has suggested some other way to send emails using System.Net.Mail. – Ganesh Sanap May 19 at 10:30
  • 1
    @Genesh thanks for the reply, but the API for the system.Net.Mail is saying This API is now obsolete. @ docs.microsoft.com/en-us/dotnet/api/… – SharePoint TestDev May 19 at 10:38
  • I am not sure but I think you will need to allow external sharing for you site. until then keep digging. – Ganesh Sanap May 19 at 11:04
0

You can use SendGrid if you want to send emails to outside of your O365 tenant. They have a free plan if the emails are limited to 100 per day.

You will find code examples in their GitHub repository. Following is one example to send emails using their API through C#.

// using SendGrid's C# Library
// https://github.com/sendgrid/sendgrid-csharp
// From: https://sendgrid.com/docs/for-developers/sending-email/v3-csharp-code-example/
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("test@example.com", "Example User");
            var subject = "Sending with SendGrid is Fun";
            var to = new EmailAddress("test@example.com", "Example User");
            var plainTextContent = "and easy to do anywhere, even with C#";
            var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
            var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response = await client.SendEmailAsync(msg);
        }
    }
}
| improve this answer | |
  • i will avoid using third party tools unless there is no other way to send email to external users. and yes we have more than 100 email per day – SharePoint TestDev May 19 at 10:22

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.