WebMail Helper - One of many useful ASP.NET Web helpers.
The role of the WebMail helper is to make sending emails simple. For detailed usage, please refer to this article.
The WebMail helper makes sending mail easier by following SMTP (Simple Mail Transfer Protocol) from Web applications.
To demonstrate how to use email, we will create an input page that lets the user submit a page to another page and send an email about a support issue.
If you have created a Demo application during this tutorial, you already have a page called _AppStart.cshtml with the following content:
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); }
To start the WebMail helper, add the following WebMail property to your AppStart page:
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); WebMail.SmtpServer = "smtp.example.com";WebMail.SmtpPort = 25;WebMail.EnableSsl = false;WebMail .UserName = "[email protected]";WebMail.Password = "password-goes-here";WebMail.From = "[email protected]"; }
Property explanation:
SmtpServer: The name of the SMTP server used to send email.
SmtpPort: The port used by the server to send SMTP transactions (email).
EnableSsl: The value is true if the server uses SSL (Secure Socket Layer) encryption.
UserName: The name of the SMTP email account used to send emails.
Password: The password for the SMTP email account.
From: The email displayed in the From address field (usually the same as UserName).
Then create an input page and name it Email_Input:
<!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 purpose of the input page is to send the information, and then submit the data to a new page that can send the information as an email.
Next create a page for sending emails and name it Email_Send:
@{ // Read input var customerEmail = Request["customerEmail"]; var customerRequest = Request["customerRequest"]; try { // Send email WebMail.Send(to:"[email protected]", subject: "Help request from - " + customerEmail, body: customerRequest ); } catch (Exception ex ) { <text>@ex</text> } }
To learn more about sending email from ASP.NET Web Pages applications, check out the WebMail Object Reference Manual.