public SendResult Send(ElasticemailMessage msg)
        {
            var result = new SendResult();

            try
            {
                string validationErrors;
                if (!IsValid(msg, out validationErrors))
                {
                    result.ResultType = ResultType.Error;
                    result.ErrorMessage = validationErrors;
                    return result;
                }

                var client = new WebClient();
                var values = new NameValueCollection();
                values.Add("api_key", _configuration.ApiKey);
                values.Add("from", msg.From.Address);
                values.Add("from_name", msg.From.DisplayName);
                values.Add("to", string.Join(";", msg.To.Select(x => x.Address)));
                values.Add("subject", msg.Subject);
                values.Add(msg.IsBodyHtml ? "body_html" : "body_text", msg.Body);
                if (msg.ReplyTo != null) values.Add("reply_to", msg.ReplyTo.Address);

                var attachmentIds = new List<string>();
                foreach (var attachment in msg.Attachments)
                {
                    var attId = UploadAttachment(attachment.Key, attachment.Value);
                    attachmentIds.Add(attId);
                }
                if (attachmentIds.Any()) values.Add("attachments", string.Join(";", attachmentIds));

                var response = new RetryLogic(_configuration.PreferredRetryStrategy).Execute(() => client.UploadValues("https://api.elasticemail.com/mailer/send", values));
                var responseString = Encoding.UTF8.GetString(response);

                Guid guid;
                if (Guid.TryParse(responseString, out guid))
                {
                    result.TransactionId = guid;
                    result.ResultType = ResultType.Success;
                }
                else
                {
                    result.ErrorMessage = responseString;
                    result.ResultType = ResultType.Error;
                }
            }
            catch (Exception ex)
            {
                result.ResultType = ResultType.Error;
                result.ErrorMessage = ex.ToString();
            }

            return result;
        }
        private bool IsValid(ElasticemailMessage msg, out string validationErrors)
        {
            var errors = new List <string>();

            if (msg.From == null)
            {
                errors.Add("sender address is missing");
            }
            if (!msg.To.Any())
            {
                errors.Add("recipient address is missing");
            }

            validationErrors = String.Join(Environment.NewLine, errors);
            return(!errors.Any());
        }
        public SendResult Send(ElasticemailMessage msg)
        {
            var result = new SendResult();

            try
            {
                string validationErrors;
                if (!IsValid(msg, out validationErrors))
                {
                    result.ResultType   = ResultType.Error;
                    result.ErrorMessage = validationErrors;
                    return(result);
                }

                var client = new WebClient();
                var values = new NameValueCollection();
                values.Add("api_key", _configuration.ApiKey);
                values.Add("from", msg.From.Address);
                values.Add("from_name", msg.From.DisplayName);
                values.Add("to", string.Join(";", msg.To.Select(x => x.Address)));
                values.Add("subject", msg.Subject);
                values.Add(msg.IsBodyHtml ? "body_html" : "body_text", msg.Body);
                if (msg.ReplyTo != null)
                {
                    values.Add("reply_to", msg.ReplyTo.Address);
                }

                var attachmentIds = new List <string>();
                foreach (var attachment in msg.Attachments)
                {
                    var attId = UploadAttachment(attachment.Key, attachment.Value);
                    attachmentIds.Add(attId);
                }
                if (attachmentIds.Any())
                {
                    values.Add("attachments", string.Join(";", attachmentIds));
                }

                var response       = new RetryLogic(_configuration.PreferredRetryStrategy).Execute(() => client.UploadValues("https://api.elasticemail.com/mailer/send", values));
                var responseString = Encoding.UTF8.GetString(response);

                Guid guid;
                if (Guid.TryParse(responseString, out guid))
                {
                    result.TransactionId = guid;
                    result.ResultType    = ResultType.Success;
                }
                else
                {
                    result.ErrorMessage = responseString;
                    result.ResultType   = ResultType.Error;
                }
            }
            catch (Exception ex)
            {
                result.ResultType   = ResultType.Error;
                result.ErrorMessage = ex.ToString();
            }

            return(result);
        }
        private bool IsValid(ElasticemailMessage msg, out string validationErrors)
        {
            var errors = new List<string>();
            if (msg.From == null)
            {
                errors.Add("sender address is missing");
            }
            if (!msg.To.Any())
            {
                errors.Add("recipient address is missing");
            }

            validationErrors = String.Join(Environment.NewLine, errors);
            return !errors.Any();
        }