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( "my.mail@mail.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();
    }
  • have you configured SMTP, or exchange server for that? – Dikesh Gandhi Apr 22 '16 at 8:57
18

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("from@mail.com", "to@mail.com");
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>{"a@mail.com"};
    emailp.To = new List<string>{"b@mail.com"};
    emailp.From = "from@mail.com";
    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: 'from@mail.com',
            To: { 'results': ['one@mail.com','two@mail.com'] },
            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:

  • 1
    Just a minor note: Your example "Using SharePoint JavaScript Model (JSOM)" is not JSOM. It is JavaScript invoking the REST / OData interface. – pholpar Aug 10 '16 at 11:01
  • Can we change the From address while using Sharepoint Utility.SentMail – user27178 Mar 7 '17 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 Mar 17 '17 at 7:51
  • I got it. sharepoint.stackexchange.com/a/210833/62326 – ADC Mar 17 '17 at 13:41

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.