<%@ Page Language="vb" AutoEventWireup="false" Codebehind="subscribe.aspx.vb" 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 NAME="txtEmailAddress"><input type=submit value="submit"> <asp:Literal ID=litMsg Runat=server></asp:Literal> </form> </body> </HTML>
Imports aspNetMX Public Class subscribe Inherits System.Web.UI.Page Protected WithEvents litMsg As System.Web.UI.WebControls.Literal Protected WithEvents txtEmailAddress As System.Web.UI.HtmlControls.HtmlInputText #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Page.IsPostBack Then 'check email address 'get an instance of MXValidate from the HttpCache Dim mx As MXValidate = GetMXObject() 'check to see if the email address is valid Dim email As String = txtEmailAddress.Value Try Dim level As MXValidateLevel = mx.Validate(email, MXValidateLevel.MXRecords) If level = MXValidateLevel.MXRecords Then '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." End If Catch 'probably dns server not available, record the email address anyway, and double check when dns 'server is back up SaveEmailAddress(email) End Try End If End Sub Private Sub SaveEmailAddress(ByVal EmailAddress As String) 'this function is a placeholder and is would be used to save EmailAddress 'to your data store. End Sub '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 Function GetMXObject() As MXValidate Dim mx As MXValidate = CType(Cache("MXValidateObject"), MXValidate) If mx Is Nothing Then '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 End If Return mx End Function 'GetMXObject End Class