Search This Blog

Thursday, November 10, 2011

Adding a Contact Us Form and sending email with ASP.NET 4.0 and C#

Feed back form in asp.net 4.0 using gmail account for sending mail.


This tutorial will demonstrate how to add a simple Contact Us form for users to leave feedback via email in ASP.NET and C#. 

Setting Up the Form 
At this point in the tutorial I have created a new ASP.NET Empty Web Site in Microsoft Visual Studio and have added in a blank Web Form named Default.aspx. What we need to do now is setup a simple form to collect the data from the user. This will include a Name, Subject, and Message. Also, we will need a submit button that the user will click to send the email, and a label to show a result if the email was sent or not.

To do this, open up Default.aspx to source mode and add the following code in the div tag:
<h2>Contact Us</h2>
<br />
<table>
    <!-- Name -->
    <tr>
        <td align="center">
            Name:</td>
        <td>
            <asp:TextBox ID="txtName" 
                            runat="server"
                            Columns="50"></asp:TextBox>
        </td>
    </tr>
 
    <!-- Subject -->
    <tr>
        <td align="center">
            Subject:
        </td>
        <td>
            <asp:DropDownList ID="ddlSubject" runat="server">
                <asp:ListItem>Ask a question</asp:ListItem>
                <asp:ListItem>Report a bug</asp:ListItem>
                <asp:ListItem>Customer support ticket</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
 
    <!-- Message -->
    <tr>
        <td align="center">
            Message:
        </td>
        <td>
            <asp:TextBox ID="txtMessage" 
                            runat="server"
                            Columns="40"
                            Rows="6" 
                            TextMode="MultiLine"></asp:TextBox>
        </td>
    </tr>
 
    <!-- Submit -->
    <tr align="center">
        <td colspan="2">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                onclick="btnSubmit_Click" />
        </td>
    </tr>
            
    <!-- Results -->
    <tr align="center">
        <td colspan="2">
            <asp:Label ID="lblResult" runat="server"></asp:Label>
        </td>
    </tr>
</table>

If you then open the Default.aspx page up to design mode you should see a table that looks similar to this: 

SS1.gif

Adding the Code 
The next thing we need to do is add the code to send the data from the form to our email address in the event that the submit button is clicked. To do this, double click the submit button to generate the Click event method for that button. Now in the Default.aspx.cs file, we will want to add in a few using statements that will allow us to send this email. Under the other using statements, add the following:
using System.Net.Mail;
using System.Net;

Next, we need to add some code to the Button's Click event method. The following code is a template for sending an email using a Gmail account, however you will need to customize this based on the RecipientAddress, which will be the email you are sending the message to, the SenderAddress, which will be the email account you will be sending the message from and also the SMTP server and port that are appropriate for your email provider.
try
{
    //Create the msg object to be sent
    MailMessage msg = new MailMessage();
    //Add your email address to the recipients
    msg.To.Add("RecipientAddress@gmail.com");
    //Configure the address we are sending the mail from
    MailAddress address = new MailAddress("SenderAddress@gmail.com");
    msg.From = address;
    //Append their name in the beginning of the subject
    msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
    msg.Body = txtMessage.Text;
 
    //Configure an SmtpClient to send the mail.
    SmtpClient client = new SmtpClient("smtp.gmail.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("SenderAddress@gmail.com""xxxx");
    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.";
}

No comments :

Post a Comment