subscribe.aspx HTML Source

 

<%@ Page language="c#" Codebehind="subscribe.aspx.cs" AutoEventWireup="false" Inherits="aspNetMXTest.subscribe" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body >

<form id="Form1" method="post" runat="server">
Please enter your email address to subscribe to our newsletter.
<br>
<input type=text id=txtEmailAddress runat=server><input type=submit value="submit">
<asp:Literal ID=litMsg Runat=server></asp:Literal>
</form>

</body>
</HTML>

 

 

subscribe.aspx.cs Source

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using aspNetMX;
namespace aspNetMXTest
{
	public class subscribe : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal litMsg;
		protected System.Web.UI.HtmlControls.HtmlInputText txtEmailAddress;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if( Page.IsPostBack) //check email address
			{
				//get an instance of MXValidate from the HttpCache
				MXValidate mx = GetMXObject();
				
				//check to see if the email address is valid
				string email = txtEmailAddress.Value;
				try
				{
					MXValidateLevel level = mx.Validate( email, MXValidateLevel.MXRecords );
					if( level == MXValidateLevel.MXRecords )
					{
						//save to the database
						SaveEmailAddress( email );
						
						//write out a friendly message
						litMsg.Text ="Thank you for subscribing";
					}
					else
					{
						litMsg.Text = "Please enter a valid email address.";
					}

				}
				catch
				{
					//probably dns server not available, record the email address anyway, and double check when dns
					//server is back up

					SaveEmailAddress( email );
					litMsg.Text ="Thank you for subscribing";

				}
			}
				
		}

		private void SaveEmailAddress( string EmailAddress)
		{
			//this function is a placeholder and is would be used to save EmailAddress
			//to your data store.
		}

		//check to see if the MX object is already stored in cache
		//if not create it. This will allow us to take advantage of MXValidate's built in MX Cache
		private MXValidate GetMXObject()
		{
			MXValidate mx = (MXValidate)Cache["MXValidateObject"];
			
			if( mx == null )
			{
				//create the mx object
				mx = new MXValidate();
				//increase the internal MX cache to be 1 day
				mx.CacheMXTimeOut = TimeSpan.FromDays( 1.0 );

				//add MXValidate to the HttpCache
				Cache["MXValidateObject"] = mx;
			}

			return mx;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}