public ActionResult Edit([Bind(Include = "Id,FromEmail,Receiver,ReadReceiptTo,Subject,Body,Created,CreatedBy")] BroadcastingEmail broadcastingEmail)
        {
            if (ModelState.IsValid)
            {
                broadcastingEmail.Body            = HttpUtility.HtmlDecode(broadcastingEmail.Body);
                broadcastingEmail.Created         = DateTime.Now;
                broadcastingEmail.CreatedBy       = System.Web.HttpContext.Current.User.Identity.Name.ToString();
                broadcastingEmail.Sent            = true;
                db.Entry(broadcastingEmail).State = EntityState.Modified;
                db.SaveChanges();
                SendEmail(broadcastingEmail);
                return(RedirectToAction("Index"));
            }
            else
            {
                var errors = ModelState.Where(x => x.Value.Errors.Any())
                             .Select(x => new { x.Key, x.Value.Errors });

                var sender = db.FromEmails.ToList();
                ViewBag.Senders = sender;

                var receiver = db.ToEmails.ToList();
                ViewBag.ToEmail = receiver;
            }
            return(View(broadcastingEmail));
        }
        public ActionResult TemplateSave([Bind(Include = "Id,FromEmail,Receiver,ReadReceiptTo,Subject,Body,Created,CreatedBy,Sent")] BroadcastingEmail broadcastingEmail)
        {
            Template template = new Template();

            if (ModelState.IsValid)
            {
                template.Html      = HttpUtility.HtmlDecode(broadcastingEmail.Body);
                template.Receiver  = broadcastingEmail.Receiver;
                template.FromEmail = broadcastingEmail.FromEmail;
                template.Subject   = broadcastingEmail.Subject;
                template.Name      = broadcastingEmail.Subject;
                template.CreatedBy = System.Web.HttpContext.Current.User.Identity.Name.ToString();
                db.Templates.Add(template);
                db.SaveChanges();

                return(RedirectToAction("Index", "Templates"));
            }
            else
            {
                var errors = ModelState.Where(x => x.Value.Errors.Any())
                             .Select(x => new { x.Key, x.Value.Errors });
            }

            return(View(broadcastingEmail));
        }
        public JsonResult SendTest(string body, string subject)
        {
            try
            {
                BroadcastingEmail broadcastingEmail = new BroadcastingEmail();
                //string tempReceiver = broadcastingEmail.Receiver;
                //string tempFromEmail = broadcastingEmail.FromEmail;
                broadcastingEmail.Receiver  = System.Web.HttpContext.Current.User.Identity.Name.ToString().Substring(6) + "@dot.gov";
                broadcastingEmail.FromEmail = System.Web.HttpContext.Current.User.Identity.Name.ToString().Substring(6) + "@dot.gov";
                broadcastingEmail.Body      = body;
                broadcastingEmail.Subject   = subject;
                broadcastingEmail.Created   = DateTime.Now;
                broadcastingEmail.CreatedBy = System.Web.HttpContext.Current.User.Identity.Name.ToString();

                SendEmail(broadcastingEmail);

                return(Json(new
                {
                    status = "Test Email Sent.",
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    status = "Error in Sending Test Email.",
                }, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult Draft([Bind(Include = "Id,FromEmail,Receiver,ReadReceiptTo,Subject,Body,Created,CreatedBy,Sent")] BroadcastingEmail broadcastingEmail)
        {
            if (ModelState.IsValid)
            {
                broadcastingEmail.Body      = HttpUtility.HtmlDecode(broadcastingEmail.Body);
                broadcastingEmail.Created   = DateTime.Now;
                broadcastingEmail.CreatedBy = System.Web.HttpContext.Current.User.Identity.Name.ToString();
                broadcastingEmail.Sent      = false;

                if (broadcastingEmail.Id > 0)
                {
                    db.Entry(broadcastingEmail).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else
                {
                    db.BroadcastingEmails.Add(broadcastingEmail);
                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                var errors = ModelState.Where(x => x.Value.Errors.Any())
                             .Select(x => new { x.Key, x.Value.Errors });
            }

            return(View(broadcastingEmail));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            BroadcastingEmail broadcastingEmail = db.BroadcastingEmails.Find(id);

            db.BroadcastingEmails.Remove(broadcastingEmail);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        // GET: Email/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BroadcastingEmail broadcastingEmail = db.BroadcastingEmails.Find(id);

            if (broadcastingEmail == null)
            {
                return(HttpNotFound());
            }
            return(View(broadcastingEmail));
        }
        private void SendEmail(BroadcastingEmail mail)
        {
            if (string.IsNullOrWhiteSpace(mail.FromEmail))
            {
                throw new Exception("Email-From cannot be empty.");
            }

            if (string.IsNullOrWhiteSpace(mail.Receiver))
            {
                throw new Exception("Email-To cannot be empty.");
            }

            SmtpClient  smtpClient = new SmtpClient();
            MailMessage message    = new MailMessage();

            if (!string.IsNullOrWhiteSpace(mail.ReadReceiptTo))
            {
                message.Headers.Add("Disposition-Notification-To", mail.ReadReceiptTo);
            }

            MailAddress fromAddress = new MailAddress(mail.FromEmail);

            message.From = fromAddress;

            message.Subject = mail.Subject;

            message.IsBodyHtml = true;

            string baseurl = System.Web.HttpContext.Current.Session["AppURL"].ToString();

            //baseurl = baseurl.Substring(0, baseurl.LastIndexOf('/') + 1); //HttpContext.Current.Session["AppURL"]

            message.Body = mail.Body;

            message.Body = mail.Body +
                           @"<div><img alt='' width='1' height='1' src='"
                           + baseurl
                           + "Image/"
                           + mail.Id
                           + "\' /></div>";

            foreach (var address in mail.Receiver.Split(';'))
            {
                message.Bcc.Add(new MailAddress(address, ""));
            }

            smtpClient.Send(message);
        }
        // GET: Email/Create
        public ActionResult Create()
        {
            var sender = db.FromEmails.ToList();

            ViewBag.Senders = sender;

            var receiver = db.ToEmails.ToList();

            ViewBag.ToEmail = receiver;

            ViewBag.BodyHtml = "";

            BroadcastingEmail broadcastingemail = new BroadcastingEmail();

            if (!String.IsNullOrEmpty(Request.QueryString["templateId"]))
            {
                int templateId = Convert.ToInt32(Request.QueryString["templateId"]);
                //var TextareaHtml = db.Templates.Where(x => x.ID == templateId).Select(x => x.Html).FirstOrDefault();
                var templateData = db.Templates.Where(x => x.ID == templateId).FirstOrDefault();
                ViewBag.BodyHtml = "";

                broadcastingemail.Body      = templateData.Html;
                broadcastingemail.FromEmail = templateData.FromEmail;
                broadcastingemail.Receiver  = templateData.Receiver;
                broadcastingemail.Subject   = templateData.Subject;
            }

            if (!String.IsNullOrEmpty(Request.QueryString["emailId"]))
            {
                //int templateId = Convert.ToInt32(Request.QueryString["templateId"]);
                //var TextareaHtml = db.Templates.Where(x => x.ID == templateId).Select(x => x.Html).FirstOrDefault();
                //ViewBag.BodyHtml = "";


                //broadcastingemail.Body = TextareaHtml;
                //broadcastingemail.FromEmail = "*****@*****.**";

                int emailId = Convert.ToInt32(Request.QueryString["emailId"]);
                broadcastingemail = db.BroadcastingEmails.Find(emailId);
            }



            //var model = new FromEmail();
            //model.Senders = new List<WebApplication1.Models.FromEmail>();

            return(View(broadcastingemail));
        }
        public ActionResult Popup(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BroadcastingEmail broadcastingEmail = db.BroadcastingEmails.Find(id);

            if (broadcastingEmail == null)
            {
                return(HttpNotFound());
            }

            //var sender = db.FromEmails.ToList();
            //ViewBag.Senders = sender;

            //var receiver = db.ToEmails.ToList();
            //ViewBag.ToEmail = receiver;

            return(PartialView("Popup", broadcastingEmail));
        }