7

I have utility class to send email.

currently i am facing error :-

a recipient must be specified

EmailProperties emailp = new EmailProperties();

emailp.To = new string[] {
    "robbstark@gmail.com"
};

emailp.From = email;
emailp.Body = "<b>Hello</b>";
emailp.Subject = "New Email";


Utility.SendEmail(clientContext, emailp);
clientContext.ExecuteQuery();
5

The default SPUtility, SendMail functionality in SharePoint is limited. And you can only send emails to people who are known within the site collection. If you only want to send email to people within site collection, then make sure you use EnsureUser() to check the availibility in site collection.

Snippet:

private void SendEmail(ClientContext clientContext)
{
    User sendToUser = clientContext.Web.EnsureUser("robbstark@gmail.com");
    clientContext.Load(sendToUser);
    clientContext.ExecuteQuery();

    string email = Microsoft.SharePoint.Client.Utilities.Utility.GetCurrentUserEmailAddresses(clientContext).Value;

    Microsoft.SharePoint.Client.Utilities.EmailProperties properties = new Microsoft.SharePoint.Client.Utilities.EmailProperties();
    properties.To = new string[] { sendToUser.Email };
    properties.Subject = "subject";
    properties.Body = "body";

    Microsoft.SharePoint.Client.Utilities.Utility.SendEmail(clientContext, properties);

    clientContext.ExecuteQuery();
}

Refer this good article by Manas Bhardwaj for more.

  • your Code Snippet , is working but only for some particular user, not for all users – Akshay Dattatray Nangare Dec 9 '15 at 10:23
  • Make sure those users are known to the site collection that you are using with clientContext. – Pradip R. Dec 9 '15 at 11:43
  • yes i have checked , my Current user is known to site collection.. – Akshay Dattatray Nangare Dec 9 '15 at 11:48
3

@Pradip already mentioned that it is not possible to send email to the external people using Utility so can consider System.Net.Mail to serve your purpose.

MailAddress from = new MailAddress("jane@contoso.com", "Jane " + (char) 0xD8 + " Clayton", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("ben@contoso.com");
MailMessage message = new MailMessage(from, to);

Find full reference in MSDN

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.