public bool Flush()
 {
     var result = 0;
     lock (this) {
         if (CurrentEmailSendRequest != null && CurrentEmailSendRequest.messages.Count > 0) {
             var response = HttpClient.SendEmails(CurrentEmailSendRequest);
             OnTranmission(response);
             result = CurrentEmailSendRequest.messages.Count;
             CurrentEmailSendRequest = new BatchEmailSendRequest();
         }
         if (CurrentTemplateSendRequest != null && CurrentTemplateSendRequest.messages.Count > 0) {
             var response = HttpClient.SendEmails(CurrentTemplateSendRequest);
             OnTranmission(response);
             result = CurrentTemplateSendRequest.messages.Count;
             CurrentTemplateSendRequest = new BatchTemplateSendRequest();
         }
     }
     Logger.info(String.Format("Flush Complete: {0} messages tranmitted.", result));
     return result > 0;
 }
        private bool Send(BatchEmailMessage email)
        {
            lock (this) {
                var result = 0;
                if (CurrentEmailSendRequest == null) {
                    CurrentEmailSendRequest = new BatchEmailSendRequest();
                }

                CurrentEmailSendRequest.messages.Add(email);

                if (CurrentEmailSendRequest.messages.Count >= EmailBufferSize) {
                    var response = HttpClient.SendEmails(CurrentEmailSendRequest);
                    OnTranmission(response);
                    result = CurrentEmailSendRequest.messages.Count;
                    CurrentEmailSendRequest = new BatchEmailSendRequest();
                }
                Logger.info(String.Format("Send Complete: {0} messages buffered and {1} messages tranmitted.", 1, result));
                return result > 0;
            }
        }
        public void MakesAValidApiRequest()
        {
            SetupDefaultExpectations();

            var testRequest = new BatchEmailSendRequest {
            };

            testRequest.messages.Add(new BatchEmailMessage {
                fromEmail = "*****@*****.**",
                fromName = "Test Sender",
                tags = new[] { "test", "test2" },
                toEmail = "*****@*****.**",
                subject = "Test Subject",
                plaintextBody = "Plain Text",
                htmlBody = "<html><body>HTML</body></html>"
            });

            testRequest.messages[0].customHeaders.Add("Test", "Header");

            var testResponse = new BatchEmailResponse {
                statusMessage = "OK",
                successCount = 1,
                failureCount = 0,
                results = new[] {
                    new BatchEmailResult { messageStatus = 0, messageId = "1234ABCD1234ABCD", toEmail = "*****@*****.**"}
                }
            };

            ResponseString = new JavaScriptSerializer().Serialize(testResponse);

            var response = Client.SendEmails(testRequest);
            Assert.AreEqual("https://api.messagebus.com/api/v3/emails/send", Client.GetArgumentsForCallsMadeOn(x => x.CreateRequest(Arg<String>.Is.Anything, Arg<SimpleHttpClient.HttpMethod>.Is.Anything))[0][0]);
            var expectedJson = @"{""messages"":[{""toEmail"":""*****@*****.**"",""fromEmail"":""*****@*****.**"",""toName"":null,""fromName"":""Test Sender"",""subject"":""Test Subject"",""plaintextBody"":""Plain Text"",""htmlBody"":""\u003chtml\u003e\u003cbody\u003eHTML\u003c/body\u003e\u003c/html\u003e"",""customHeaders"":{""Test"":""Header""},""tags"":[""test"",""test2""]}]}";
            Assert.AreEqual(expectedJson, HttpUtility.UrlDecode(RequestString));
            Assert.AreEqual(testResponse.statusMessage, response.statusMessage);
            Assert.AreEqual(testResponse.successCount, response.successCount);
            Assert.AreEqual(testResponse.results[0].messageId, response.results[0].messageId);
        }