ASP.NET Web Pages - WebMail Helper
- Previous Page WebPages Chart
- Next Page WebPages PHP
WebMail Helper - one of many useful ASP.NET Web helpers.
WebMail Helper
The WebMail Helper makes it easier to send emails using SMTP from web applications.
Script: Email Support
To demonstrate the use of email, we will create an input page for technical support, allowing users to submit this page to another page and then send an email about a support issue.
First: edit your AppStart page
If you have built the DEMO application in this tutorial, there should be an _AppStart.cshtml page in the site with the following content:
_AppStart.cshtml
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); }
To initialize the WebMail helper, add the following WebMail properties to your AppStart page:
_AppStart.cshtml
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); WebMail.SmtpServer = "smtp.example.com"; WebMail.SmtpPort = 25; WebMail.EnableSsl = false; WebMail.UserName = "support@example.com"; WebMail.Password = "password-goes-here"; WebMail.From = "john@example.com"; }
Property Explanation:
SmtpServer: Name of the SMTP server used to send emails.
SmtpPort: Server port used to send SMTP transactions (emails).
EnableSsl: True if the server should use SSL (Secure Socket Layer) encryption.
UserName: Name of the SMTP email account used to send emails.
Password: Password for the SMTP email account.
From: The email address appearing in the from field (usually the same as UserName).
Second: create an email input page
Then create an input page named Email_Input:
Email_Input.cshtml
<!DOCTYPE html> <html> <body> <h1>Request for Assistance</h1> <form method="post" action="EmailSend.cshtml"> <label>Username:</label> <input type="text name="customerEmail" /> <label>Details about the problem:</label> <textarea name="customerRequest" cols="45" rows="4"></textarea> <p><input type="submit" value="Submit" /></p> </form> </body> </html>
The input page is used to collect information and then submit the data to a new page that can send the information as an email.
Third: Create the Email Sending Page
Then create a page for sending emails named Email_Send:
Email_Send.cshtml
@{ // Read input var customerEmail = Request["customerEmail"]; var customerRequest = Request["customerRequest"]; try { // Send email WebMail.Send(to:"someone@example.com", subject: "Help request from - " + customerEmail, body: customerRequest ); } catch (Exception ex ) { <text>@ex</text> } }
For more information on sending emails from an ASP.NET Web Pages application, please refer to:WebMail Object Reference Manual.
- Previous Page WebPages Chart
- Next Page WebPages PHP