How do I send an email message in a SharePoint Hosted application?

I am working on a SharePoint hosting application and am trying to send an email with client code. Should I be using Workflow or Event Receiver for this? Can't seem to find any information on this.

Any ideas?

+3


source to share


3 answers


You can add Workflow from Visual Studio to your project. Drag email from the toolbar to the workflow, configure the settings, and the workflow is now set up to send email.



On the client side, you need to enable sp.workflowservices.js

. Then all you have to do is get an instance of the workflow and start it.

+2


source


You can use this feature to send emails using JavaScript from a SharePoint-hosted application.



function sendEmail(from, to, body, subject) {
    var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";
    $.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': from,
                'To': { 'results': [to] },
                //'CC': { 'results': [cc]}, //Maybe you want to include a CC address?
                'Body': body,
                'Subject': subject
            }
        }
      ),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
           alert("An email was sent.");
        },
        error: function (args) {
           alert("We had a problem and an email was not sent.");
        }
    });
}

      

+2


source


You won't be able to send an actual client-side email, but you can create an email and send it to an ASP.Net page that will send the email.

See How to send email with JavaScript for an example.

+1


source







All Articles