Monday, April 5, 2010

Sending Email from your website, Feed back form, ASP.NET, C#

I was thinking to make the email part better in ByJoel.com, before it was just a link to Microsoft Outlook. I've added the email form and set up the SMTP, now I can receive feedback directly to my inbox. see here : www.byjoel.com/FeedBack.aspx



Sending feed back email WebForm controls:
I have built the User Interface using ASP.Net WebForm controls as shown above. As you can see there are four text boxes, one multi line text box and a Submit button. I have also applied proper ASP.Net Validation controls to avoid errors. 
The "Name", "Email" and "Subject" text boxes are normal text boxes where user can insert the values. but "To" text box has read only property and email address of our web admin, so that user can not change the "To" address.      


Code: 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage objEmail = new MailMessage();
        objEmail.To = txtTo.Text;
        objEmail.From = txtEmail.Text;
        objEmail.Subject = txtSub.Text;
        objEmail.Body = "Name: " + txtName.Text + "

" + "Comments: " + txtComments.Text; objEmail.Priority = MailPriority.High; objEmail.BodyFormat = MailFormat.Html; // Make sure you have appropriate replying permissions from your local system. // or contact your web hosting and get the SMTP server name, I have added our SMTP server name SmtpMail.SmtpServer = "localhost"; try { SmtpMail.Send(objEmail); Response.Redirect("OnSale.aspx"); //for testing you can add Response.Write("Feed back sent"); //But it is not advised for working website, So i have redirected to other page. } catch (Exception exc) { Response.Redirect("Default.aspx"); //here exception object created but never used, //it can be used as Response.Write("Send failure: " + exc.ToString()); } }

No comments:

Post a Comment