10

I am trying to write a mailsender in C# for SharePoint but each time I start it I get this error message that tells me that it doesn't have permission.

I had tried to give it permission at AppManifest.xml but I don't know which permission will work for this? I tried all but nothing seams to help.

My code to send email:

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();
    }
1
  • have you configured SMTP, or exchange server for that? Commented Apr 22, 2016 at 8:57

1 Answer 1

19

There are several alternative approaches available to send Emails.

  • General Email Sending method
  • SharePoint Client Object Model (CSOM)
  • SharePoint JavaScript Model (JSOM)

Using general Email Sending method

This is the usual method to send email in asp.net. There are advantages over the other two methods.

  • Send attachments to the recipients
  • Send emails to external users (SharePoint 2013 email function can not be used to send emails to external users)

code:

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Using SharePoint Client Object Model (CSOM)

This is a commonly used method to send email. You can use a SharePoint Utility class to send an Email. One downside is that you cannot send to external users. If you are sending to external users they should be added to your mail exchange. That needs to be done in advance since it will take some time to reflect such changes.

code:

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.From = "[email protected]";
    emailp.Body = "<b>html</b>";
    emailp.Subject = "subject";
    Utility.SendEmail(_clientContext, emailp);
    _clientContext.ExecuteQuery();
}

Using JavaScript and the SharePoint REST-API

This is very similar to the CSOM but it will use only JavaScript for sending emails.

code:

var mail = {
        properties: {
            __metadata: { 'type': 'SP.Utilities.EmailProperties' },
            From: '[email protected]',
            To: { 'results': ['[email protected]','[email protected]'] },
            Body: 'some body',
            Subject: 'subject'
        }
    };

var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
var urlTemplate = getAppWebUrlUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
            contentType: 'application/json',
            url: urlTemplate,
            type: "POST",
            data: JSON.stringify(mail),
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {

                // code

            },

            error: function (err) {

                // code

            }
        });

Reference:

4
  • 1
    Just a minor note: Your example "Using SharePoint JavaScript Model (JSOM)" is not JSOM. It is JavaScript invoking the REST / OData interface.
    – pholpar
    Commented Aug 10, 2016 at 11:01
  • Can we change the From address while using Sharepoint Utility.SentMail
    – user27178
    Commented Mar 7, 2017 at 10:06
  • I have the following issue. From JS code in the add-in app (SP online) I am calling the SP Utilities to send an email to users, in the same SP domain obviously (not external accounts). The function is working correctly only with users who are part of the current group which is operating on the site collection where the add-in is installed. If I try to specify recipients not added in the group they are cut out from the email. When I try to add the wished user accounts at the group then they get the email.
    – ADC
    Commented Mar 17, 2017 at 7:51
  • I got it. sharepoint.stackexchange.com/a/210833/62326
    – ADC
    Commented Mar 17, 2017 at 13:41

Your Answer

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