aspNetEmail

EmailMessage.ProcessASPNETForm Method 

Processes an ASP.NET form with specially named controls, for easy email sending.

public void ProcessASPNETForm(
   Control page,
   bool send
);

Parameters

page
The control container
send
Immediately send the email after processing.

Remarks

This method will loop through the specificed control, normally of type System.Web.Page, to locate specially named controls. If a specially named control is found aspNetEmail will set the value of the control to the respective aspNetEmail property (see the table below for special names and values ). If the specially named controls are found their Text or Value will be mapped to the specific property. aspNetEmail will check each control's name, and if it matches, aspNetEmail will resolve values in the following order:

  1. If a Server Control is found, and a Text property exists, the Text value will be used.
  2. If a Server Control is found, and a Text property does NOT exist, and IF the control has a Value property, then the value of Value will be used.
  3. If a Server Control cannot be found, the Request collection is checked for an object with that name. If the named object is found, its respective text value is used.

The following control names will map to their respective aspNetEmail.EmailMessage properties.

Control NameRelated aspNetEmail.EmailMessage Property/Method
aemAltTextSets the EmailMessage.AltText property.
aemBodySets the EmailMessage.Body property
aemCharSetSets the EmailMessage.CharSet property.
aemBodyFormatSets the EmailMessage.BodyFormat property ( "html" or "text" )
aemContentTransferEncodingSets the EmailMessage.ContentTransferEncoding property
aemHtmlBodyPartSets the HTML text for the EmailMessage.HtmlBodyPart
aemTextBodyPartSets the text for the EmailMessage.TextBodyPart
aemSubjectSets the text for the EmailMessage.Subject
aemPrioritySets the EmailMessage.Priority value.

Used in conjunction with EmailMessage.LoadFromConfig, you can easily process your emails in a just a few lines of code.

The following example demonstrates how to create a common 'Contact Us' communication webform found on most websites. As part of the sample, a hidden HTML object, and a server side control are used.

The a hidden, client side HTML object is used to contain the subject, and is aptly named 'aemSubject', while the body of the email will come from the textarea server control, and is named appropriately, 'aemBody'.

Example

[C#]
            
            <%@ Page language="c#" %>
            <%@ Import Namespace="aspNetEmail" %>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
            <script runat=server>
            
            		private void Page_Load(object sender, System.EventArgs e)
            		{
            			if( Page.IsPostBack )
            			{
            				EmailMessage msg = new EmailMessage();
            				msg.LoadFromConfig();
            				msg.ProcessASPNETForm( this, true );
            				litThankYou.Text = "Thank you for your comments.";
            			}
            
            		}
            </script>
            <HTML>
              <HEAD>
                <title>contactus</title>
            </HEAD>
              <body >
            
            	<h3>Contact Us</h3>
            	<p>Please use the form below to submit your comments to our website</p>	
            	<asp:Literal ID=litThankYou Runat=server></asp:Literal>
            
                <form id="example_contactus" method="post" runat="server">
                <input type=hidden value="Website Comments" name="aemSubject" >
            
            	<br><textarea id=aemBody cols=30 rows=6 runat=server NAME="aemBody"></textarea>
            	
            	<br><input type=submit value=Submit>
                 </form>
            	
              </body>
            </HTML>
            
            The web.config has the following entries
            
            
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            	<appSettings>
            		<add key="EmailMessage.To" value="you@mycompany.com"></add>
            		<add key="EmailMessage.FromAddress" value="me@mycompany.com"></add>
            		<add key="EmailMessage.Server" value="mail.mycompany.com"></add>
            	</appSettings>
              <system.web>
             </system.web>
            </configuration>
            
            
[Visual Basic]
            
            
            <%@ Page language="VB" %>
            <%@ Import Namespace="aspNetEmail" %>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
            <script runat=server>
            
            		Sub Page_Load( sender as object, e as System.EventArgs)
            			if( Page.IsPostBack )
            			
            				Dim msg as New EmailMessage()
            				msg.LoadFromConfig()
            				msg.ProcessASPNETForm( Me, true )
            				litThankYou.Text = "Thank you for your comments."
            				
            			End If
            		End Sub
            </script>
            <HTML>
              <HEAD>
                <title>contactus</title>
            </HEAD>
              <body >
            
            	<h3>Contact Us</h3>
            	<p>Please use the form below to submit your comments to our website</p>	
            	<asp:Literal ID=litThankYou Runat=server></asp:Literal>
            
                <form id="example_contactus" method="post" runat="server">
                <input type=hidden value="Website Comments" name="aemSubject" >
            
            	<br><textarea id=aemBody cols=30 rows=6 runat=server NAME="aemBody"></textarea>
            	
            	<br><input type=submit value=Submit>
                 </form>
            	
              </body>
            </HTML>
            
            
            The web.config has the following entries
            
            
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            	<appSettings>
            		<add key="EmailMessage.To" value="you@mycompany.com"></add>
            		<add key="EmailMessage.FromAddress" value="me@mycompany.com"></add>
            		<add key="EmailMessage.Server" value="mail.mycompany.com"></add>
            	</appSettings>
              <system.web>
             </system.web>
            </configuration>
            
            
            

See Also

EmailMessage Class | aspNetEmail Namespace