SPS中使用JSOM發郵件

欣靜賞悅發表於2016-09-30

直接上程式碼了:

        function ShowMailDialog() {

            $.ajax({
                url: siteurl + "/_api/contextinfo",
                method: "POST",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    alert(data.d.GetContextWebInformation.FormDigestValue);
                    $('#__REQUESTDIGEST').val(data.d.GetContextWebInformation.FormDigestValue)

                    var from = 'Yang.Liu4@xxxx.com',
                        to = 'Yang.Liu4@xxxx.com',
                        body = 'Hello World Body',
                        subject = 'Hello World Subject';

                    // Call sendEmail function
                    sendEmail(from, to, body, subject);
                },
                error: function (data, errorCode, errorMessage) {
                    alert(errorMessage)
                }
            });

            return;
        }
        function sendaEmail(from, to, body, subject) {
            //Get the relative url of the site
            var _ctx = SP.ClientContext.get_current();
            var _spPageContextInfo = _ctx.get_web();
                //_spPageContextInfo.webServerRelativeUrl;
            var urlTemplate = siteurl + "/_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]
                        },
                        'Body': body,
                        'Subject': subject
                    }
                }),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
                },
                success: function (data) {
                    alert('Email Sent Successfully');
                },
                error: function (err) {
                    alert('Error in sending Email: ' + JSON.stringify(err));
                }
            });
        }
View Code

相關文章