public JsonResult Create(EConfigEmail model)
        {
            using (var db = new MyDbDataContext())
            {
                if (db.ConfigWebsites.Any())
                {
                    return(Json(new { Result = "ERROR", Message = "Website chỉ được phép tồn tại 1 bản cấu hình" }));
                }
                if (ModelState.IsValid)
                {
                    try
                    {
                        ConfigWebsite configWebsite = new ConfigWebsite
                        {
                            Host     = model.Host,
                            Port     = model.Port,
                            Email    = model.Email,
                            Password = CryptorEngine.Encrypt(model.Password, true),
                        };
                        db.ConfigWebsites.InsertOnSubmit(configWebsite);
                        db.SubmitChanges();

                        model.ID = configWebsite.ID;
                        return(Json(new { Result = "OK", Message = "Thêm thành công", Record = model }));
                    }
                    catch (Exception exception)
                    {
                        return(Json(new { Result = "ERROR", Message = "Fail. Error:" + exception.Message }));
                    }
                }
                return(Json(new { Result = "ERROR", Errors = ModelState.Errors(), Message = "Dữ liệu đầu vào không đúng định dạng" }));
            }
        }
Exemple #2
0
        public static bool SendMail(string toEmail, string subject, string content)
        {
            try
            {
                using (var db = new MyDbDataContext())
                {
                    ConfigWebsite configWebsite = db.ConfigWebsites.FirstOrDefault() ?? new ConfigWebsite();
                    Hotel         hotel         = db.Hotels.FirstOrDefault() ?? new Hotel();
                    string        host          = configWebsite.Host;
                    int           port          = configWebsite.Port;
                    string        email         = configWebsite.Email;
                    string        password      = CryptorEngine.Decrypt(configWebsite.Password, true);

                    var smtpClient = new SmtpClient(host, port)
                    {
                        UseDefaultCredentials = false,
                        Credentials           = new NetworkCredential(email, password),
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        EnableSsl             = true,
                        Timeout = 100000
                    };

                    var mail = new MailMessage
                    {
                        Body    = content,
                        Subject = subject,
                        From    = new MailAddress(hotel.Email, hotel.Name)
                    };

                    mail.To.Add(new MailAddress(toEmail));
                    mail.BodyEncoding = Encoding.UTF8;
                    mail.IsBodyHtml   = true;
                    mail.Priority     = MailPriority.High;

                    smtpClient.Send(mail);

                    return(true);
                }
            }
            catch (SmtpException)
            {
                return(false);
            }
        }