ASP.Net使用响应重定向,具体取决于电子邮件是否已发送

ASP.Net使用响应重定向,具体取决于电子邮件是否已发送,asp.net,webforms,Asp.net,Webforms,我有一个4pg流程,第三页是我的确认页。此页面有一个按钮,用于发送和发送电子邮件。然后,无论电子邮件是否已发送,我都会有一个响应。重定向。但我想做的是,如果电子邮件已发送,则转到下一页,但如果无法发送,则在页面上显示并显示错误,而不是重定向 我不知道该怎么做。我的密码是 protected void pg3button_Click(object sender, EventArgs e) { try {

我有一个4pg流程,第三页是我的确认页。此页面有一个按钮,用于发送和发送电子邮件。然后,无论电子邮件是否已发送,我都会有一个
响应。重定向
。但我想做的是,如果电子邮件已发送,则转到下一页,但如果无法发送,则在页面上显示并显示错误,而不是重定向

我不知道该怎么做。我的密码是

protected void pg3button_Click(object sender, EventArgs e)
        {
            try
            {
                //Create the msg object to be sent
                MailMessage msg = new MailMessage();

                //Add your email address to the recipients
                msg.To.Add("test@test.com");

                //Configure the address we are sending the mail from
                MailAddress address = new MailAddress("test@test.com");
                msg.From = address;

                //Append their name in the beginning of the subject
                msg.Subject = "Enquiry";

                msg.Body = Label1.Text + " " + Session["pg1input"].ToString()
                            + Environment.NewLine.ToString() +
                            Label2.Text + " " + Session["pg1dd"].ToString()
                            + Environment.NewLine.ToString() +
                            Label3.Text + " " + Session["pg2"].ToString();

                //Configure an SmtpClient to send the mail.
                SmtpClient client = new SmtpClient("smtp.live.com", 587);
                client.EnableSsl = true; //only enable this if your provider requires it

                //Setup credentials to login to our sender email address ("UserName", "Password")
                NetworkCredential credentials = new NetworkCredential("test@test.com", "Password");
                client.Credentials = credentials;

                //Send the msg
                client.Send(msg);

                //Display some feedback to the user to let them know it was sent
                lblResult.Text = "Your message was sent!";

                //Clear the form
                //txtName.Text = "";
                //txtMessage.Text = "";
            }
            catch
            {
                //If the message failed at some point, let the user know
                lblResult.Text = "Your message failed to send, please try again.";
            }
                Response.Redirect("/Session/Pg4.aspx");                
        }

移动
Response.Redirect(“/Session/Pg4.aspx”)
块内尝试
块。

您只想在愉快的路径中重定向(无例外)

移动
Response.redirect(“/Session/Pg4.aspx”)
块内尝试
块。
您只希望在愉快的路径中重定向(无例外)