+ Reply to Thread
Results 1 to 2 of 2

Thread: Page redirect after email is sent

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    30

    Page redirect after email is sent

    Hi , i am creating a method to send mail and after mail is sent i need to redirect to a custom page , anyway here is my code :
    Code:
    protected void SubmitBtn_Click(object sender,EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
           EmailTxt.Text, "0000@mail.com", SubjectTxt.Text, MessageTxt.Text);
        SmtpClient SMTPServer = new SmtpClient("localhost");
        try
        {
            SMTPServer.Send(mailObj);
            Response.Redirect("sucess.aspx");
            base.OnLoad(e);
            MessageLbl.Text = "Email Sent SucessFully!!";
        }
        catch (Exception ex)
        {
            MessageLbl.Text = ex.ToString();
        }
    }

  2. #2
    The Response.Redirect is your problem. You are effectively refreshing the page and any code after that is irrelevant. Why not just clear the form via clode (i.e. myTextBox.Text = string.Empty etc. and then set the message text?
    Code:
    try
    {
        SMTPServer.Send(mailObj);
        // Clear email form
        Control1.Text = string.Empty;
        Control2.Text = string.Empty;
        // etc...
        MessageLbl.Text = "Email Sent SucessFully!!!";
    }
    catch (Exception ex)
    {
        MessageLbl.Text = ex.ToString();
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts