This is my first post, so bear with me.

I’m currently working to create a website for the start-up software company Higher Symmetry Software. At the moment, Higher Symmetry has yet to officially release their first product, so in the meantime, we’re trying to determine a way to easily gauge user interest. I came up with the simple idea of creating an ASP.NET webform that takes in a user’s email address and generates an automatic email that will be sent to an email distribution list. So, my first post will focus on creating a form to generate and send an email using C# and System.Web.Mail.

The html (aspx) side is fairly simple. Just a form element with a textbox and a button. In an aspx page, that looks like this:

<form id="Form1" method="post" runat="server">
   
<div align="center" style="padding: 15px" id="submitemail" runat="server">
        Interested in Product X?
<br /><br />
        Enter your email address below to get updates as they become available!<
br /><br />
        <
asp:TextBox ID="txtEmailUpdates" Runat="server" style="width: 200px" /><br /><br />
        <
asp:Button ID="btnSubmit" Runat="server" Text="Submit" />
    </
div>
   
<div align="center" style="padding: 15px" id="emailsent" runat="server" visible="false">
        Thanks for your support!  We will keep you updated!
   
</div>
</form>

If you notice here, I’ve enclosed the main body of the form within one div and the success message within another div. I prefer this to using an asp.net placeholder control simply for the flexibility that div tags have with CSS. I’ll demonstrate how to show and hide these divs later in the post.

The code-behind page is a little more complex, but still fairly straightforward. First, import the libraries you’ll need. In this case, all you need are System and System.Web.Mail. Next, make sure that you have declared all of your controls (or have Visual Studio auto-generate them for you) and that your btnSubmit_Click method has been declared and created. In that method, you’ll need something similar to the following:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
   
string strTo = "you@email.com";
   
string strFrom = txtEmailUpdates.Text.Trim();
   
string strSubject = "Website submission"; </p>

    </font>try
   
{
       
SmtpMail.SmtpServer = "smtp.yourserver.com";
       
SmtpMail.Send(strFrom, strTo, strSubject,String.Format("{0} is interested in Product X.", txtEmailUpdates.Text.Trim()));
   
}
   
catch( Exception )
   
{
       
//Do nothing — just let them assume they submitted the email
    } </p>

    </font>submitemail.Visible = false;
   
emailsent.Visible = true;
} </div>


That should be it. To make things more consistent across the application, I like to put the smtp server and the email address in the web.config file, but it’s up to you.