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[] {
    "[email protected]"
};

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


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

2 Answers 2

4

@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("[email protected]", "Jane " + (char) 0xD8 + " Clayton", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("[email protected]");
MailMessage message = new MailMessage(from, to);

Find full reference in MSDN

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("[email protected]");
    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.

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

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.