ASP.NET Web Pages - WebMail Object
Through the WebMail object, you can easily send email from a web page.
Description
WebMail ObjectProvides ASP.NET Web Pages with the ability to send email using SMTP (Simple Mail Transfer Protocol).
Example
Please see the WebPages Email examples in the chapter.
WebMail Object Reference - Properties
| property | Description |
|---|---|
| SmtpServer | The name of the SMTP server used to send email. |
| SmtpPort | The port the server uses to send SMTP email. |
| EnableSsl | True if the server uses SSL (Secure Socket Layer) encryption. |
| UserName | The name of the SMTP email account used to send email. |
| Password | The password of the SMTP email account. |
| From | The email displayed in the From address field (usually the same as UserName). |
WebMail Object Reference - Methods
| Method | Description |
|---|---|
| Send() | Sends the email message to be delivered to the SMTP server. |
The Send() method has the following parameters:
| Parameters | Type | Description |
|---|---|---|
| to | String | Recipient (separated by semicolons) |
| subject | String | Email subject |
| body | String | Email body |
The Send() method has the following optional parameters:
| Parameters | Type | Description |
|---|---|---|
| from | String | Sender |
| cc | String | Email addresses to carbon copy (separated by semicolons) |
| filesToAttach | Collection | Attachment name |
| isBodyHtml | Boolean | True if the email body is in HTML format |
| additionalHeaders | Collection | Additional headers |
Technical data
| Name | Value |
|---|---|
| Class | System.Web.Helpers.WebMail |
| Namespace | System.Web.Helpers |
| Assembly | System.Web.Helpers.dll |
Initializing the WebMail Helper
To use the WebMail helper, you must be able to access an SMTP server. SMTP is the "outgoing" part of email. If you use a virtual host, you may already know the name of the SMTP server. If you use a corporate network, your company's IT department will give you a name. If you work from home, you may be able to use a regular email service provider.
To send an email, you will need:
- The name of the SMTP server
- The port number (usually 25)
- The email username
- The email password
In your web root, create a page named _AppStart.cshtml (if it already exists, simply edit the page).
Copy the following code into the file:
_AppStart.cshtml
@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "support@example.com";
WebMail.Password = "password";
WebMail.From = "john@example.com"
}
The code above will run every time the website (application) starts. It assigns initial values to WebMail Object.
Please replace:
data to the child component via smtp.example.com with the name of the SMTP server you will use to send email.
data to the child component via 25 with the port number the server uses to send SMTP transactions (email).
If the server uses SSL (Secure Socket Layer) encryption, replace false with true.
data to the child component via support@example.com with the name of the SMTP email account used to send email.
data to the child component via password with the password of the SMTP email account.
Replace **john@example ** with the email displayed in the From address field.
![]() |
In your AppStart file, you do not need to start the WebMail Object, but before calling the WebMail.Send() method, you must set these properties. |
|---|
