/// <summary>
        /// Asynchronously sends a bulk email message and returns the response from the Injection API.
        /// </summary>
        /// <param name="message">A <c>BulkMessage</c> object to be sent.</param>
        /// <param name="cancellationToken">A <c>CancellationToken</c> to handle cancellation between async threads.</param>
        /// <returns>A <c>SendResponse</c> of an SocketLabsClient send request.</returns>
        /// <example>
        /// This sample shows you how to Send a Bulk Message
        /// <code>
        /// var client = new SocketLabsClient(00000, "apiKey");
        ///
        /// var message = new BulkMessage();
        ///
        /// message.PlainTextBody = "This is the body of my message sent to ##Name##";
        /// message.HtmlBody = "<html>This is the HtmlBody of my message sent to ##Name##</html>";
        /// message.Subject = "Sending a test message";
        /// message.From.Email = "*****@*****.**";
        ///
        /// var recipient1 = message.To.Add("*****@*****.**");
        /// recipient1.MergeData.Add("Name", "Recipient1");
        ///
        /// var recipient2 = message.To.Add("*****@*****.**");
        /// recipient2.MergeData.Add("Name", "Recipient2");
        ///
        /// var response = await client.Send(message);
        ///
        /// if (response.Result != SendResult.Success)
        /// {
        ///     // Handle Error
        /// }
        ///</code>
        /// </example>
        public async Task <SendResponse> SendAsync(IBulkMessage message, CancellationToken cancellationToken)
        {
            var validator = new SendValidator();

            var validationResult = validator.ValidateCredentials(_serverId, _apiKey);

            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            validationResult = validator.ValidateMessage(message);
            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            var factory          = new InjectionRequestFactory(_serverId, _apiKey);
            var injectionRequest = factory.GenerateRequest(message);
            var json             = injectionRequest.GetAsJson();

            var retryHandler = new RetryHandler(_httpClient, EndpointUrl, new RetrySettings(NumberOfRetries));
            var httpResponse = await retryHandler.SendAsync(json, cancellationToken);

            var response = new InjectionResponseParser().Parse(httpResponse);

            return(response);
        }
예제 #2
0
        /// <summary>
        /// Asynchronously sends a bulk email message and returns the response from the Injection API.
        /// </summary>
        /// <param name="message">A <c>BulkMessage</c> object to be sent.</param>
        /// <returns>A <c>SendResponse</c> of an SocketLabsClient send request.</returns>
        /// <example>
        /// This sample shows you how to Send a Bulk Message
        /// <code>
        /// var client = new SocketLabsClient(00000, "apiKey");
        ///
        /// var message = new BulkMessage();
        ///
        /// message.PlainTextBody = "This is the body of my message sent to ##Name##";
        /// message.HtmlBody = "<html>This is the HtmlBody of my message sent to ##Name##</html>";
        /// message.Subject = "Sending a test message";
        /// message.From.Email = "*****@*****.**";
        ///
        /// var recipient1 = message.To.Add("*****@*****.**");
        /// recipient1.MergeData.Add("Name", "Recipient1");
        ///
        /// var recipient2 = message.To.Add("*****@*****.**");
        /// recipient2.MergeData.Add("Name", "Recipient2");
        ///
        /// var response = await client.Send(message);
        ///
        /// if (response.Result != SendResult.Success)
        /// {
        ///     // Handle Error
        /// }
        ///</code>
        /// </example>
        public async Task <SendResponse> SendAsync(IBulkMessage message)
        {
            var validator = new SendValidator();

            var validationResult = validator.ValidateCredentials(_serverId, _apiKey);

            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            validationResult = validator.ValidateMessage(message);
            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            var factory          = new InjectionRequestFactory(_serverId, _apiKey);
            var injectionRequest = factory.GenerateRequest(message);

            var httpResponse = await _httpClient.PostAsync(EndpointUrl, injectionRequest.GetAsJson());

            var response = new InjectionResponseParser().Parse(httpResponse);

            return(response);
        }