aspNetEmail

EmailMessage.FormToDataTable Method (Control, HtmlFormOptions)

Converts an ASP.NET/HTML form to a DataTable that can be used for a MailMerge

public static DataTable FormToDataTable(
   Control control,
   HtmlFormOptions options
);

Parameters

control
A parent ASP.NET control container (such as the System.Web.UI.Page control)
options
Different HtmlFormOptions to include when building the DataTable

Return Value

ASP.NET/HTML Form as a DataTable

Example

[C#]
            private void Page_Load(object sender, System.EventArgs e)
            {
            	if( Page.IsPostBack )
            	{
            		//email the form
            		EmailMessage msg = new EmailMessage( "mail.myCompany.com");
            		msg.FromAddress = "me@myCompany.com";
            
            		//enable in-memory logging
            		msg.Logging = true;
            		msg.LogInMemory = true;
            
            		//The FormToDataTable() will create a 1 row datatable that can be used for a mail merge
            		//The names of the controls found on the page will be created as column names of the DataTable
            		DataTable dt = EmailMessage.FormToDataTable( this.Page, HtmlFormOptions.All );
            
            		//use the txtEmailAddress, txtFirstName, txtLastName controls to create a mail merge
            		msg.To = "##txtEmailAddress##";
            		msg.Subject = "Thank you ##txtFirstName## ##txtLastName##";
            		msg.Body = "Hi ##txtFirstName##,\r\n Thanks for your comments. Here is the information you request...";
            		
            		try
            		{
            			msg.SendMailMerge( dt );
            		}
            		catch
            		{
            			//for troubleshooting purposes, write out the log
            			//remove in production so the user won't see it
            			Response.Write( msg.GetLog( true ) );
            		}
            	}
            }
            
            
            
            
            
            
[VB.NET]
            Private Sub Page_Load(sender As Object, e As System.EventArgs)
               If Page.IsPostBack Then
                  'email the form
                  Dim msg As New EmailMessage("mail.myCompany.com")
                  msg.FromAddress = "me@myCompany.com"
                  
                  'enable in-memory logging
                  msg.Logging = True
                  msg.LogInMemory = True
                  
                  'The FormToDataTable() will create a 1 row datatable that can be used for a mail merge
                  'The names of the controls found on the page will be created as column names of the DataTable
                  Dim dt As DataTable = EmailMessage.FormToDataTable(Me.Page, HtmlFormOptions.All)
                  
                  'use the txtEmailAddress, txtFirstName, txtLastName controls to create a mail merge
                  msg.To = "##txtEmailAddress##"
                  msg.Subject = "Thank you ##txtFirstName## ##txtLastName##"
                  msg.Body = "Hi ##txtFirstName##," + ControlChars.Cr + ControlChars.Lf + " Thanks for your comments. Here is the information you request..."
                  
                  Try
                     msg.SendMailMerge(dt)
                  Catch
                  End Try
               End If 'for troubleshooting purposes, write out the log
            End Sub 'Page_Load 'remove in production so the user won't see it
            
            

See Also

EmailMessage Class | aspNetEmail Namespace | EmailMessage.FormToDataTable Overload List