コード例 #1
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>
        /// <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);
        }
コード例 #3
0
        public void ValidateCredentials_ReturnsSuccess_WhenApiKeyAndServerIdIsNotEmpty()
        {
            //Arrange
            var serverId  = RandomHelper.RandomServerId();
            var apiKey    = RandomHelper.RandomString();
            var validator = new SendValidator();

            //Act
            var actual = validator.ValidateCredentials(serverId, apiKey);

            //Assert
            Assert.AreEqual(SendResult.Success, actual.Result);
        }
コード例 #4
0
        public void ValidateCredentials_ReturnsAuthenticationError_WhenApiKeyIsNotEmptyAndServerIdIsEmpty()
        {
            //Arrange
            const int serverId  = int.MinValue;
            var       apiKey    = RandomHelper.RandomString();
            var       validator = new SendValidator();

            //Act
            var actual = validator.ValidateCredentials(serverId, apiKey);

            //Assert
            Assert.AreEqual(SendResult.AuthenticationValidationFailed, actual.Result);
        }