Processes an ASP.NET form with specially named controls, for easy email sending.
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:
The following control names will map to their respective aspNetEmail.EmailMessage properties.
| Control Name | Related aspNetEmail.EmailMessage Property/Method |
|---|---|
| aemAltText | Sets the EmailMessage.AltText property. |
| aemBody | Sets the EmailMessage.Body property |
| aemCharSet | Sets the EmailMessage.CharSet property. |
| aemBodyFormat | Sets the EmailMessage.BodyFormat property ( "html" or "text" ) |
| aemContentTransferEncoding | Sets the EmailMessage.ContentTransferEncoding property |
| aemHtmlBodyPart | Sets the HTML text for the EmailMessage.HtmlBodyPart |
| aemTextBodyPart | Sets the text for the EmailMessage.TextBodyPart |
| aemSubject | Sets the text for the EmailMessage.Subject |
| aemPriority | Sets 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'.
[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>