public void Post()
        {
            string body = "";

            try
            {
                using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true))
                    body = sr.ReadToEnd();

                FlowrouteMmsMessage msg         = FlowrouteMmsMessage.FromJson(body);
                List <Attachment>   attachments = new List <Attachment>();
                string     toPhone    = msg.Data.Attributes.To;
                string     fromPhone  = msg.Data.Attributes.From;
                HttpClient httpClient = new HttpClient();
                if (msg.Included != null)
                {
                    foreach (var file in msg.Included)
                    {
                        byte[] bytes = httpClient.GetByteArrayAsync(file.Attributes.Url).Result;
                        attachments.Add(new Attachment(new MemoryStream(bytes), file.Attributes.FileName, file.Attributes.MimeType));
                    }
                }
                var routes = _context.IncomingRoutes.Where(r => r.Phone == toPhone);
                if (routes.Count() > 0)
                {
                    EmailService.SendEmail(routes.Select(r => r.Email).ToArray(), $"{fromPhone}@example.com", $"MMS Message From {fromPhone}", msg.Data.Attributes.Body ?? "No body text", false, attachments);
                }
                //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API MMS Request", JsonConvert.SerializeObject(msg), false);
            }
            catch (Exception ex)
            {
                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API Error", ExcDetails.Get(ex), false);
                if (!String.IsNullOrWhiteSpace(body))
                {
                    EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "MMS Receipt Body for Error", body, false);
                }
            }
        }
        public void Post()
        {
            string body = "";

            try
            {
                using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true))
                    body = sr.ReadToEnd();

                FlowrouteGetMessageResponse msg = JsonConvert.DeserializeObject <FlowrouteGetMessageResponse>(body);
                var routes = _context.IncomingRoutes.Where(r => r.Phone == msg.Data.Attributes.To);
                if (routes.Count() > 0)
                {
                    EmailService.SendEmail(routes.Select(r => r.Email).ToArray(), $"{msg.Data.Attributes.From}@example.com", $"SMS Message From {msg.Data.Attributes.From}", msg.Data.Attributes.Body, false);

                    //FlowrouteClient client = new FlowrouteClient(Settings.FlowrouteAccessKey, Settings.FlowrouteSecretKey);
                    //var result = await client.Messaging.SendMessageAsync(msg.Data.Attributes.From, msg.Data.Attributes.To, "Message received!");

                    //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API Request", body, false);
                    //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API Response", JsonConvert.SerializeObject(result), false);
                }
            }
            catch (Exception ex)
            {
                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API Error", ExcDetails.Get(ex), false);
            }
        }
        public void Post()
        {
            string body = "";

            try
            {
                using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true))
                    body = sr.ReadToEnd();

                dynamic mb = JsonConvert.DeserializeObject(body);
                if (mb.notificationType != null && mb.notificationType == "AmazonSnsSubscriptionSucceeded")
                {
                    return;
                }
                Message msg = Message.ParseMessage(body);
                if (msg.IsMessageSignatureValid())
                {
                    if (msg.IsSubscriptionType)
                    {
                        HttpClient client = new HttpClient();
                        _ = client.GetAsync(msg.SubscribeURL).Result;
                        return;
                    }
                    if (msg.IsNotificationType)
                    {
                        //dynamic m = JsonConvert.DeserializeObject(msg.MessageText);
                        //dynamic mail = m.mail;
                        dynamic m = JsonConvert.DeserializeObject(msg.MessageText);
                        if (m.notificationType == "Delivery")
                        {
                            return;
                        }
                        byte[] content     = Convert.FromBase64String((string)m.content);
                        var    message     = MimeMessage.Load(new MemoryStream(content));
                        string smsContent  = message.TextBody;
                        string fromAddress = ((MailboxAddress)message.From[0]).Address;

                        var fromPhone = _context.OutgoingRoutes.Where(r => r.Email == fromAddress).Select(r => r.Phone).FirstOrDefault();
                        if (String.IsNullOrWhiteSpace(fromPhone))
                        {
                            return;
                        }

                        string toPhone = ((MailboxAddress)message.To[0]).Address;
                        toPhone = toPhone.Substring(0, toPhone.IndexOf("@"));

                        // TODO: Send error message back if toPhone is incorrect format or fromAddress not authorized in lookup table

                        FlowrouteClient client = new FlowrouteClient(Settings.FlowrouteAccessKey, Settings.FlowrouteSecretKey);
                        var             result = client.Messaging.SendMessageAsync(Digitize(toPhone), fromPhone, smsContent.Trim()).Result;

                        if (!result.Success)
                        {
                            EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Flowroute API Response", JsonConvert.SerializeObject(result), false);
                        }

                        //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Email Received", msgBody, false);
                        //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SMS Content", smsContent.Trim() + " - " + Digitize(toPhone) + " - " + fromAddress + " - " + fromPhone, false);
                        //EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Non-Bounce Notification", msgBody, false);
                    }
                    else
                    {
                        EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Non-NotificationType", body, false);
                    }
                }
            }
            catch (Exception ex)
            {
                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES SMS Receipt Error", ExcDetails.Get(ex), false);
                if (!String.IsNullOrWhiteSpace(body))
                {
                    EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES SMS Receipt Body for Error", body, false);
                }
            }
        }
        public void Post()
        {
            string body = "";

            try
            {
                using (StreamReader sr = new StreamReader(Request.Body))
                    body = sr.ReadToEnd();

                Message msg = Message.ParseMessage(body);
                if (String.IsNullOrWhiteSpace(msg?.Signature))
                {
                    dynamic m = JsonConvert.DeserializeObject(body);
                    if (m.notificationType != null && m.notificationType == "AmazonSnsSubscriptionSucceeded")
                    {
                        return;
                    }
                }
                if (msg.IsMessageSignatureValid())
                {
                    if (msg.IsSubscriptionType)
                    {
                        HttpClient client = new HttpClient();
                        _ = client.GetAsync(msg.SubscribeURL).Result;
                        return;
                    }
                    if (msg.IsNotificationType)
                    {
                        dynamic m    = JsonConvert.DeserializeObject(msg.MessageText);
                        dynamic mail = m.mail;
                        if (m.notificationType == "Bounce")
                        {
                            dynamic b = m.bounce;
                            //if (b.bounceSubType != "General" && b.bounceSubType != "Suppressed" && b.bounceSubType != "MailboxFull")
                            //{
                            //    EmailService.SendEmail(new[] { Settings.BounceReporting, Settings.ErrorReporting }, Settings.DefaultFrom, "SES Non-General Bounce", body, false);
                            //    goto END;
                            //}
                            foreach (dynamic recipient in b.bouncedRecipients)
                            {
                                string bounceBody    = "<p>Following are the details of the email bounce. Ensure this email exists or remove it from the system that sent the email.</p>";
                                string bouncedEmails = "";
                                foreach (dynamic em in b.bouncedRecipients)
                                {
                                    if (bouncedEmails.Length > 0)
                                    {
                                        bouncedEmails += "," + (string)em.emailAddress;
                                    }
                                    else
                                    {
                                        bouncedEmails = (string)em.emailAddress;
                                    }
                                }
                                bounceBody += $"<p><strong>Email(s):</strong> {bouncedEmails}</p>";
                                bounceBody += $"<p><strong>Bounce Type:</strong> {b.bounceType}*</p>";
                                bounceBody += $"<p><strong>Bounce Sub Type:</strong> {b.bounceSubType}*</p>";
                                bounceBody += $"<p><strong>Original Subject:</strong> {(string)mail.commonHeaders.subject}</p>";
                                bounceBody += $"<p><strong>Date Sent:</strong> {(string)mail.commonHeaders.date}</p>";
                                bounceBody += "<p>* <strong>Transient</strong> means temporary issue. This email address should be verified. This could indicate a full inbox or vacation mode.<br />";
                                bounceBody += "* <strong>Permanent</strong> means this email address does not exist. Remove this address from the system that sent it. Contact [email protected] if you need assistance.</p>";
                                bounceBody += "<h2 style='margin-top:30px;'>Headers</h2><ul>";

                                foreach (dynamic h in mail.headers)
                                {
                                    bounceBody += $"<li><strong>{h.name}:</strong> {h.value}</li>";
                                }

                                bounceBody += "</ul>";

                                EmailService.SendEmail(new[] { Settings.BounceReporting }, Settings.DefaultFrom, "Bounce From example.com", bounceBody);
                                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES example.com Bounce", bounceBody);
                                return;
                            }
                        }
                        else if (m.notificationType == "Complaint")
                        {
                            try
                            {
                                dynamic c = m.complaint;

                                string complaintBody    = "<p>Following are the details of the email complaint. <span style='font-weight:bold;color:#f00;'>Automatic action is not available at this time.</span> Please take the appropriate action (eg - disable email, reach out to customer, etc).</p>";
                                string complainedEmails = "";
                                foreach (dynamic em in c.complainedRecipients)
                                {
                                    if (complainedEmails.Length > 0)
                                    {
                                        complainedEmails += "," + (string)em.emailAddress;
                                    }
                                    else
                                    {
                                        complainedEmails = (string)em.emailAddress;
                                    }
                                }
                                complaintBody += $"<p><strong>Complaint Type:</strong> {c.complaintFeedbackType}</p>";
                                complaintBody += $"<p><strong>Email(s):</strong> {complainedEmails}</p>";
                                complaintBody += $"<p><strong>Original Subject:</strong> {(string)mail.commonHeaders.subject}</p>";
                                complaintBody += $"<p><strong>Date Sent:</strong> {(string)mail.commonHeaders.date}</p>";

                                complaintBody += "<h2 style='margin-top:30px;'>Headers</h2><ul>";

                                foreach (dynamic h in mail.headers)
                                {
                                    complaintBody += $"<li><strong>{h.name}:</strong> {h.value}</li>";
                                }

                                complaintBody += "</ul>";


                                EmailService.SendEmail(new[] { Settings.BounceReporting }, Settings.DefaultFrom, "Email Complaint Notification", complaintBody);
                                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "Complaint Notification", complaintBody);
                            }
                            catch (Exception ex)
                            {
                                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Complaint Processing Error", ExcDetails.Get(ex), false);
                                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Complaint Body for Error", body, false);
                            }
                        }
                        else
                        {
                            EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Non-Bounce Notification", body, false);
                        }
                    }
                    else
                    {
                        EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Non-NotificationType", body, false);
                    }
                }
            }
            catch (Exception ex)
            {
                EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Bounce Processing Error", ExcDetails.Get(ex), false);
                if (!String.IsNullOrWhiteSpace(body))
                {
                    EmailService.SendEmail(new[] { Settings.ErrorReporting }, Settings.DefaultFrom, "SES Bounce Body for Error", body, false);
                }
            }
        }