ASP.NET Web Pages - WebMail Object
With the WebMail object, you can easily send email from a web page.
Description
WebMail ObjectProvides the ability to send email using SMTP (Simple Mail Transfer Protocol) for ASP.NET Web Pages.
Examples
Please seeWebPages Emailthe examples in the chapter.
WebMail Object Reference - Properties
| Properties | Description |
|---|---|
| SmtpServer | The name of the SMTP server used to send email. |
| SmtpPort | The port used by the server 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 for the SMTP email account. |
| From | The email displayed in the sender address field (usually the same as UserName). |
WebMail Object Reference - Methods
| Methods | Description |
|---|---|
| Send() | Sends the email message to be transmitted to the SMTP server. |
The Send() method has the following parameters:
| Parameter | Type | Description |
|---|---|---|
| to | String | Recipient (separated by semicolons) |
| subject | String | Email subject |
| body | String | Email body |
The Send() method has the following optional parameters:
| Parameter | Type | Description |
|---|---|---|
| from | String | Sender |
| cc | String | Email addresses to be copied (CC) (separated by semicolons) |
| filesToAttach | Collection | Attachment name |
| isBodyHtml | Boolean | true if the email body is HTML formatted |
| additionalHeaders | Collection | Additional headers |
Technical Data
| Name | Value |
|---|---|
| Class | System.Web.Helpers.WebMail |
| Namespace | System.Web.Helpers |
| Assembly | System.Web.Helpers.dll |
Initialize the WebMail Helper
To use the WebMail Helper, you must have access to an SMTP server. SMTP is the "outgoing" part of email. If you are using a virtual host, you may already know the name of the SMTP server. If you are working on a company 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 directory, create a page named_AppStart.cshtml(if it already exists, edit the page directly).
Copy the following code into the file:
_AppStart.cshtml
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password";
WebMail.From = "[email protected]"
}
The code above will run every time the website (application) starts. It, for theWebMail object,assigns initial values.
Please replace:
willsmtp.example.comReplace with the name of the SMTP server you will use to send email.
will25Replace with the port number the server uses to send SMTP transactions (email).
If the server uses SSL (Secure Socket Layer) encryption, pleasefalsereplace it with true.
will[email protected]Replace with the name of the SMTP email account used to send email.
willpasswordReplace with the password for the SMTP email account.
willjohn@example Replace with the email displayed in the sender address field.
| In your AppStart file, you do not need to initializethe WebMail object,but before calling theWebMail.Send()method, you must set these properties. |
Other Extensions