ASP.Net使用响应.重定向取决于如果电子邮件已发送或没有



我有一个4pg的进程,第三页是我的确认页。这个页面有一个发送和电子邮件的按钮。然后,一旦邮件发送或不发送,我就有了response.redirect。我想做的是,如果电子邮件已经发送,然后转到下一页,但如果它发送失败,然后在页面上显示和错误,而不是重定向。

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

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");移到try块中。您只希望在正常路径中重定向(没有例外)

相关内容

最新更新