Beispiel #1
0
        /// <summary>
        /// Send a message using the Mailgun API service.
        /// </summary>
        /// <param name="workingDomain">The mailgun working domain to use</param>
        /// <param name="message">The message to send</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> SendMessageAsync(string workingDomain, IMessage message)
        {
            //check for parameters
            ThrowIf.IsArgumentNull(() => workingDomain);
            ThrowIf.IsArgumentNull(() => message);


            //build request
            using (var client = new HttpClient())
            {
                var buildUri = new UriBuilder
                {
                    Host   = BaseAddress,
                    Scheme = UseSSl ? "https" : "http",
                    Path   = string.Format("{0}/messages", workingDomain)
                };


                //add authentication
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                           Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + ApiKey)));


                //set the client uri
                return(await client.PostAsync(buildUri.ToString(), message.AsFormContent()).ConfigureAwait(false));
            }
        }
        /// <summary>
        /// Send a message using the Mailgun API service.
        /// </summary>
        /// <param name="workingDomain">The mailgun working domain to use</param>
        /// <param name="message">The message to send</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> SendMessageAsync(string workingDomain, IMessage message)
        {
            //check for parameters
            ThrowIf.IsArgumentNull(() => workingDomain);
            ThrowIf.IsArgumentNull(() => message);


            //build request
            using (var client = httpClientFactory?.CreateClient("Mailgun.MessageService") ?? new HttpClient())
            {
                var buildUri = new UriBuilder
                {
                    Host   = BaseAddress,
                    Scheme = UseSSl ? "https" : "http",
                    Path   = string.Format("{0}/messages", workingDomain)
                };


                //add authentication
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                           Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + ApiKey)));


                //set the client uri
                return(await client.PostAsync(buildUri.ToString(), message.AsFormContent())
                       .ConfigureAwait(false));

                //TODO may be read answer and throw exceptions on errors.
                //or introduce some new API that will do that
            }
        }