Ejemplo n.º 1
0
        protected void butSend_Click(object sender, EventArgs e)
        {
            var contactEmail = AppEnv.ContactEmail;

            if (txtName.Text.Trim().Length == 0 || txtEmail.Text.Trim().Length == 0 || txtContent.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải điền tất cả các ô có dấu (*)");
                return;
            }
            //if(txtCode.Text.Trim() != FormShield1.GetText())
            //{
            //    MessageBox.Show("Bạn nhập không đúng mã bảo vệ");
            //    return;
            //}

            const string matchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                             + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                             + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                             + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

            if (Regex.IsMatch(txtEmail.Text.Trim(), matchEmailPattern) == false)
            {
                MessageBox.Show("Email không đúng !!!");
                return;
            }

            var sb = new StringBuilder();

            sb.Append("<b>Ho ten</b>: ");
            sb.Append(txtName.Text);
            sb.Append("<br><b>Email</b>: ");
            sb.Append(txtEmail.Text);
            sb.Append("<br><b>Dien thoai</b>:<br>");
            sb.Append(txtPhone.Text);
            sb.Append("<br><b>Gioi tinh</b>:<br>");
            sb.Append(rdolGender.SelectedValue);
            sb.Append("<br><b>Tieu de</b>:<br>");
            sb.Append(txtSubject.Text);
            sb.Append("<br><b>Content</b>:<br>");
            sb.Append(txtContent.Text);


            // new email solution start
            var email = new MailMessage(txtEmail.Text, contactEmail)
            {
                Subject    = "Lien he tu khach hang ghe tham website",
                IsBodyHtml = true,
                Body       = sb.ToString()
            };

            var smtp = new SmtpClient
            {
                Host = AppEnv.MailServer.Length == 0 ? "localhost" : AppEnv.MailServer,
                Port =
                    AppEnv.MailServerPort.Length == 0 ? 25 : ConvertUtility.ToInt32(AppEnv.MailServerPort)
            };



            // if authentication
            if (AppEnv.MailUsername.Length > 0 && AppEnv.MailPassword.Length > 0)
            {
                smtp.Credentials    = new NetworkCredential(AppEnv.MailUsername, AppEnv.MailPassword);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            }
            // if authentication end

            var info = new MailInfo
            {
                Mail_Kind     = "lienhe",
                Mail_Name     = HTMLUtility.SecureHTML(txtName.Text),
                Mail_Email    = HTMLUtility.SecureHTML(txtEmail.Text),
                Mail_Phone    = HTMLUtility.SecureHTML(txtPhone.Text),
                Mail_Address  = "",
                Mail_Content  = HTMLUtility.SecureHTML(txtContent.Text),
                Pix_ID        = 0,
                Mail_Answer   = ConvertUtility.ToBoolean(false),
                Mail_Datetime = DateTime.Now
            };

            MailDB.Insert(info);

            try
            {
                smtp.Send(email);
                notice.InnerHtml = "<br><br><br><font color=red><b>Email đã được gửi tới " + contactEmail + "...</b></font>";
            }
            catch (Exception ex)
            {
                notice.InnerHtml = "<br><br><br><font color=red><b>Email đã được gửi đi... Xin chân thành cảm ơn.</b></font>";
                ErrorReportDB.NewReport(Request.RawUrl, ex.ToString());
                //notice.InnerHtml = "<br><br><br><font color=red><b>Lỗi trong quá trình gửi mail...</b></font><br>" + ex.Message;
            }
            finally
            {
                pnform.Visible = false;
                notice.Visible = true;
            }
        }
Ejemplo n.º 2
0
        protected void butSend_Click(object sender, EventArgs e)
        {
            var contactEmail = AppEnv.ContactEmail;

            var hocsinhname     = txtHocsinhName.Text.Trim();
            var phuhuynhname    = txtPhuHuynh.Text.Trim();
            var emailregister   = txtEmail.Text.Trim();
            var phoneregister   = txtPhone.Text.Trim();
            var addressregister = txtAddress.Text.Trim();
            var noteregister    = txtContent.Text;


            if (hocsinhname.Length == 0 || phuhuynhname.Length == 0 || emailregister.Length == 0 || phoneregister.Length == 0 || addressregister.Length == 0)
            {
                MessageBox.Show("Bạn phải điền tất cả các ô có dấu (*)");
                return;
            }
            //if(txtCode.Text.Trim() != FormShield1.GetText())
            //{
            //    MessageBox.Show("Bạn nhập không đúng mã bảo vệ");
            //    return;
            //}

            const string matchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                             + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                             + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                             + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

            if (Regex.IsMatch(txtEmail.Text.Trim(), matchEmailPattern) == false)
            {
                MessageBox.Show("Email không đúng !!!");
                return;
            }

            //insert db
            var hocsinhinfo = new HocsinhInfo();

            hocsinhinfo.Hocsinh_Name     = hocsinhname;
            hocsinhinfo.Hocsinh_Parent   = phuhuynhname;
            hocsinhinfo.Hocsinh_Email    = emailregister;
            hocsinhinfo.Hocsinh_Tel      = phoneregister;
            hocsinhinfo.Hocsinh_Address  = addressregister;
            hocsinhinfo.Hocsinh_Birthday = dropDay.SelectedValue + "/" + dropMonth.SelectedValue + "/" +
                                           dropYear.SelectedValue;
            hocsinhinfo.Hocsinh_Note       = noteregister;
            hocsinhinfo.Hocsinh_CreateDate = DateTime.Now;
            hocsinhinfo.Hocsinh_IsLearning = true;

            int hocsinhId = HocsinhDB.Insert(hocsinhinfo);

            var i = 0;

            foreach (DataGridItem item in dtgClass.Items)
            {
                var chkSelect = (CheckBox)item.FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    var id = ConvertUtility.ToInt32(item.Cells[0].Text);

                    var registerInfo = new HocsinhRegisterInfo();
                    registerInfo.Hocsinh_ID   = hocsinhId;
                    registerInfo.Content_ID   = id;
                    registerInfo.RegisterTime = DateTime.Now;

                    HocsinhRegisterDB.Insert(registerInfo);

                    i = 1 + 1;
                }
            }

            //noi dung mail

            var contentmail = string.Empty;

            if (i == 0)
            {
                contentmail  = "Dang ky cho lop khai giang";
                contentmail += "<br /><br />" + noteregister;
            }
            else
            {
                contentmail = noteregister;
            }

            var sb = new StringBuilder();

            sb.Append("<b>Ho ten hoc sinh</b>: ");
            sb.Append(hocsinhname);
            sb.Append("<br /><b>Ten phu huynh</b>: ");
            sb.Append(phuhuynhname);
            sb.Append("<br /><b>Email</b>: ");
            sb.Append(emailregister);
            sb.Append("<br /><b>Dien thoai</b>:<br />");
            sb.Append(phoneregister);
            sb.Append("<br /><b>Dia chi</b>: ");
            sb.Append(addressregister);
            sb.Append("<br /><b>Ngay sinh</b>:<br />");
            sb.Append(dropDay.SelectedValue + "/" + dropMonth.SelectedValue + "/" + dropYear.SelectedValue);
            sb.Append("<br /><b>Thong tin them</b>:<br />");
            sb.Append(contentmail);


            // new email solution start
            var email = new MailMessage(txtEmail.Text, contactEmail)
            {
                Subject    = "Dang ky khoa hoc tu website",
                IsBodyHtml = true,
                Body       = sb.ToString()
            };

            var smtp = new SmtpClient
            {
                Host = AppEnv.MailServer.Length == 0 ? "localhost" : AppEnv.MailServer,
                Port =
                    AppEnv.MailServerPort.Length == 0 ? 25 : ConvertUtility.ToInt32(AppEnv.MailServerPort)
            };



            // if authentication
            if (AppEnv.MailUsername.Length > 0 && AppEnv.MailPassword.Length > 0)
            {
                smtp.Credentials    = new NetworkCredential(AppEnv.MailUsername, AppEnv.MailPassword);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            }
            // if authentication end

            var info = new MailInfo
            {
                Mail_Kind     = "lienhe",
                Mail_Name     = HTMLUtility.SecureHTML(txtHocsinhName.Text),
                Mail_Email    = HTMLUtility.SecureHTML(txtEmail.Text),
                Mail_Phone    = HTMLUtility.SecureHTML(txtPhone.Text),
                Mail_Address  = HTMLUtility.SecureHTML(txtAddress.Text),
                Mail_Content  = HTMLUtility.SecureHTML(sb.ToString()),
                Pix_ID        = 0,
                Mail_Answer   = ConvertUtility.ToBoolean(false),
                Mail_Datetime = DateTime.Now
            };

            MailDB.Insert(info);

            try
            {
                smtp.Send(email);
                notice.InnerHtml = "<br><br><br><font color=red><b>Thông tin đăng ký đã được gửi tới " + contactEmail + "...<br />Chúng tôi sẽ liên hệ lại trong thời gian ngắn nhất.<br />Xin chân thành cảm ơn.</b></font>";
            }
            catch (Exception ex)
            {
                notice.InnerHtml = "<br><br><br><font color=red><b>Thông tin đăng ký đã được gửi đi...<br />Chúng tôi sẽ liên hệ lại trong thời gian ngắn nhất.<br />Xin chân thành cảm ơn.</b></font>";
                ErrorReportDB.NewReport(Request.RawUrl, ex.ToString());
                //notice.InnerHtml = "<br><br><br><font color=red><b>Lỗi trong quá trình gửi mail...</b></font><br>" + ex.Message;
            }
            finally
            {
                pnform.Visible = false;
                notice.Visible = true;
            }
        }