Esempio n. 1
0
        /// <summary>
        /// Send out an email
        /// </summary>
        /// <param name="CreateSendEmailInput">Object containing request parameters</param>
        /// <return>Returns the string response from the API call</return>
        public string CreateSendEmail(CreateSendEmailInput input)
        {
            Task <string> t = CreateSendEmailAsync(input);

            APIHelper.RunTaskSynchronously(t);
            return(t.Result);
        }
Esempio n. 2
0
        /// <summary>
        /// Send out an email
        /// </summary>
        /// <param name="CreateSendEmailInput">Object containing request parameters</param>
        /// <return>Returns the string response from the API call</return>
        public async Task <string> CreateSendEmailAsync(CreateSendEmailInput input)
        {
            //validating required parameters
            if (null == input.To)
            {
                throw new ArgumentNullException("to", "The property \"To\" in the input object cannot be null.");
            }

            if (null == input.From)
            {
                throw new ArgumentNullException("mfrom", "The property \"From\" in the input object cannot be null.");
            }

            if (null == input.Subject)
            {
                throw new ArgumentNullException("subject", "The property \"Subject\" in the input object cannot be null.");
            }

            if (null == input.Message)
            {
                throw new ArgumentNullException("message", "The property \"Message\" in the input object cannot be null.");
            }

            //the base uri for api requestss
            string _baseUri = Configuration.GetBaseURI();

            //prepare query string for API call
            StringBuilder _queryBuilder = new StringBuilder(_baseUri);

            _queryBuilder.Append("/email/sendemails.{ResponseType}");

            //process optional template parameters
            APIHelper.AppendUrlWithTemplateParameters(_queryBuilder, new Dictionary <string, object>()
            {
                { "ResponseType", input.ResponseType }
            });


            //validate and preprocess url
            string _queryUrl = APIHelper.CleanUrl(_queryBuilder);

            //append request with appropriate headers and parameters
            var _headers = new Dictionary <string, string>()
            {
                { "user-agent", "message360-api" }
            };

            //append form/field parameters
            var _fields = new Dictionary <string, object>()
            {
                { "to", input.To },
                { "from", input.From },
                { "type", SendEmailAsHelper.ToValue(input.Type) },
                { "subject", input.Subject },
                { "message", input.Message },
                { "cc", input.Cc },
                { "bcc", input.Bcc },
                { "attachment", input.Attachment }
            };

            //prepare the API call request to fetch the response
            HttpRequest _request = ClientInstance.Post(_queryUrl, _headers, _fields, Configuration.BasicAuthUserName, Configuration.BasicAuthPassword);

            //invoke request and get response
            HttpStringResponse _response = (HttpStringResponse)await ClientInstance.ExecuteAsStringAsync(_request).ConfigureAwait(false);

            HttpContext _context = new HttpContext(_request, _response);

            //handle errors defined at the API level
            base.ValidateResponse(_response, _context);

            try
            {
                return(_response.Body);
            }
            catch (Exception _ex)
            {
                throw new APIException("Failed to parse the response: " + _ex.Message, _context);
            }
        }