Пример #1
0
        public ActionResult SendMailsL(string To, string Cc, string Bcc, string Files, string Sub, string Bod, string ContactID, bool?cctomeL)
        {
            List <SendMails> LstMail1 = new List <SendMails>();

            if (!String.IsNullOrEmpty(To))
            {
                List <string> s = To.Split(',').ToList();
                foreach (var item in s)
                {
                    if (item != "")
                    {
                        SendMails _Mail = new SendMails();

                        _Mail.toAddress = item;
                        if (!String.IsNullOrEmpty(Files))
                        {
                            _Mail.AttachedFiles  = Files;
                            _Mail.AttachedFolder = @"D:\MassMail\AttachFiles\";
                        }
                        LstMail1.Add(_Mail);
                    }
                }
            }

            if (!String.IsNullOrEmpty(Cc))
            {
                foreach (var _item in LstMail1)
                {
                    _item.ccAddress = Cc;
                    //if (cctomeL == true)
                    //    _item.ccAddress = _item.ccAddress + _Identity.Email;
                }
            }
            //else
            //{
            //    if (cctomeL == true)
            //    {
            //        foreach (var _item in LstMail1)
            //        {
            //            _item.ccAddress = _Identity.Email;
            //        }
            //    }
            //}

            if (!String.IsNullOrEmpty(Bcc))
            {
                foreach (var _item in LstMail1)
                {
                    _item.bccAddress = Bcc;
                }
            }

            var config = db.SMTPProfiles.ToList();

            #region Xu ly luu vao queue mails database
            foreach (var item in LstMail1)
            {
                //Bod = Bod.Replace("#DistributorName", dis.Name);
                QueueMails _m = new FCV_DutchLadyMail_Model.Models.QueueMails();
                if (!String.IsNullOrEmpty(ContactID))
                {
                    int id = Convert.ToInt32(ContactID);
                    _m.ContactListID = id;
                }
                _m.SMTPProfile = 1;
                _m.toAddress   = item.toAddress;
                _m.ccAddress   = item.ccAddress;
                _m.bccAddress  = item.bccAddress;
                _Identity      = (IdentityUser)Session["Identity"];
                _m.fromName    = _Identity.FullName;
                _m.fromAddress = _Identity.Address;
                _m.SentBy      = _Identity.UserName;
                var    Body = HttpUtility.HtmlDecode(Bod);
                string s    = "<meta http-equiv=\"Content-Type\"  content=\"text/html charset=UTF-8\" />";
                _m.Body    = s + Bod;
                _m.Subject = Sub;
                //if (!String.IsNullOrEmpty(Temp))
                //    _m.Template = Convert.ToInt32(Temp);
                _m.QueueTime      = DateTime.Now;
                _m.AttachedFolder = item.AttachedFolder;
                _m.AttachedFiles  = item.AttachedFiles;
                try
                {
                    db.QueueMails.Add(_m);
                    db.SaveChanges();
                }
                catch (Exception ex) { }
            }
            #endregion
            Session.Add("CountGet", 0);
            TempData["MessageCount"] = LstMail1.Count + " emails sent to queue !";
            return(RedirectToAction("SendMails"));
        }
Пример #2
0
        public ActionResult SendMails(string To, string Cc, string Bcc, string Files, string Sub, string Bod, string Temp, string AddCC, string AddBCC)
        {
            To = To.Replace("\n", ""); Cc = Cc.Replace("\n", ""); Bcc = Bcc.Replace("\n", "");
            List <SendMails> LstMail = new List <SendMails>();

            _Identity = (IdentityUser)Session["Identity"];
            if (!String.IsNullOrEmpty(To))
            {
                List <string> npp = To.Split('[').ToList();

                foreach (var item in npp)
                {
                    if (item != "")
                    {
                        SendMails     _Mail = new SendMails();
                        List <string> s     = item.Split(']').ToList();
                        string        code  = s[0];
                        _Mail.DistributorCode = code;
                        if (s[1] != "")
                        {
                            _Mail.toAddress = s[1];
                            //if (!String.IsNullOrEmpty(Files) && !Files.Equals("-- Please select --"))
                            if (!String.IsNullOrEmpty(Files))
                            {
                                int id     = Convert.ToInt32(Files);
                                var folder = db.Folders.Where(ta => ta.ID == id).SingleOrDefault();
                                _Mail.AttachedFolder = folder.Path;
                            }
                        }

                        LstMail.Add(_Mail);
                    }
                }
            }
            if (!String.IsNullOrEmpty(Cc))
            {
                List <string> npp = Cc.Split('[').ToList();

                foreach (var item in npp)
                {
                    if (item != "")
                    {
                        SendListMails _Mail = new SendListMails();
                        List <string> s     = item.Split(']').ToList();
                        string        code  = s[0];
                        var           dt    = LstMail.Where(ta => ta.DistributorCode.Equals(code)).SingleOrDefault();
                        if (s[1] != "")
                        {
                            dt.ccAddress = s[1];
                            if (!String.IsNullOrEmpty(AddCC))
                            {
                                dt.ccAddress = dt.ccAddress + AddCC;
                            }
                        }
                        else
                        {
                            dt.ccAddress = AddCC;
                        }
                    }
                }
            }
            else
            {
                List <string> npp = To.Split('[').ToList();

                foreach (var item in npp)
                {
                    if (item != "")
                    {
                        SendMails     _Mail = new SendMails();
                        List <string> s     = item.Split(']').ToList();
                        string        code  = s[0];
                        _Mail.DistributorCode = code;
                        var dt = LstMail.Where(ta => ta.DistributorCode.Equals(code)).SingleOrDefault();

                        if (!String.IsNullOrEmpty(AddCC))
                        {
                            dt.ccAddress = AddCC;
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(Bcc))
            {
                List <string> npp = Bcc.Split('[').ToList();

                foreach (var item in npp)
                {
                    if (item != "")
                    {
                        SendListMails _Mail = new SendListMails();
                        List <string> s     = item.Split(']').ToList();
                        string        code  = s[0];
                        var           dtBcc = LstMail.Where(ta => ta.DistributorCode.Equals(code)).SingleOrDefault();
                        if (s[1] != "")
                        {
                            dtBcc.bccAddress = s[1];
                            if (!String.IsNullOrEmpty(AddBCC))
                            {
                                dtBcc.bccAddress = dtBcc.bccAddress + AddBCC;
                            }
                        }
                        else
                        {
                            if (!String.IsNullOrEmpty(AddBCC))
                            {
                                dtBcc.bccAddress = AddBCC;
                            }
                        }
                    }
                }
            }
            else
            {
                List <string> npp = To.Split('[').ToList();

                foreach (var item in npp)
                {
                    if (item != "")
                    {
                        SendMails     _Mail = new SendMails();
                        List <string> s     = item.Split(']').ToList();
                        string        code  = s[0];
                        _Mail.DistributorCode = code;
                        var dt = LstMail.Where(ta => ta.DistributorCode.Equals(code)).SingleOrDefault();

                        if (!String.IsNullOrEmpty(AddBCC))
                        {
                            dt.bccAddress = AddBCC;
                        }
                    }
                }
            }


            var config = db.SMTPProfiles.ToList();

            #region Xu ly luu vao queue mails database
            foreach (var item in LstMail)
            {
                var        dis     = db.Distributors.Where(ta => ta.Code == item.DistributorCode).SingleOrDefault();
                string     BodTemp = Bod.Replace("#DistributorName", dis.Name);
                QueueMails _m      = new FCV_DutchLadyMail_Model.Models.QueueMails();
                _m.SMTPProfile     = 1;
                _m.DistributorCode = item.DistributorCode;
                _m.toAddress       = item.toAddress;
                _m.ccAddress       = item.ccAddress;
                _m.bccAddress      = item.bccAddress;
                _Identity          = (IdentityUser)Session["Identity"];
                _m.fromName        = _Identity.FullName;
                _m.fromAddress     = _Identity.Address;
                _m.SentBy          = _Identity.UserName;


                var    Body = HttpUtility.HtmlDecode(BodTemp);
                string s    = "<meta http-equiv=\"Content-Type\"  content=\"text/html charset=UTF-8\" />";
                _m.Body           = s + Body;
                _m.Subject        = Sub;
                _m.QueueTime      = DateTime.Now;
                _m.AttachedFolder = item.AttachedFolder;
                try
                {
                    db.QueueMails.Add(_m);
                    db.SaveChanges();
                }
                catch (Exception ex) { }
            }
            #endregion
            Session.Add("CountGet", 0);
            TempData["MessageCount"] = LstMail.Count + " emails sent to queue !";
            return(RedirectToAction("SendMails"));
        }