|
 |
 |
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.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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:
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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."; }
|
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Here we have added code to create a new MailMessage object that will be sent via an SmtpClient object that will be configured based on an email address that we already have. The message object is populated with the data from the form and an email address to send it to. The SmtpClient is populated with the correct SMTP settings for the provider and email you are sending the message from. Then, the message is sent and the result label is changed to the appropriate text while the rest of the form is cleared.
To test this, simply check the email address that you have sent the message to and verify that an email has been sent and its contents are correct.
|
|
|