ASP.NET Web Pages - WebMail Object
Using the WebMail object, you can easily send emails from your web pages.
Description
The WebMail object provides the functionality to send emails using SMTP (Simple Mail Transfer Protocol) in ASP.NET Web Pages.
Example
Please see the example in the WebPages Email section.
WebMail Object Reference Manual - Properties
Property | Description |
---|---|
SmtpServer | The name of the SMTP server used to send emails. |
SmtpPort | The port used by the server to send SMTP emails. |
EnableSsl | 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 sender's address field (usually the same as UserName). |
WebMail Object Reference Manual - Methods
Method | Description |
---|---|
Send() | Sends the email message to the SMTP server for delivery. |
The Send() method has the following parameters:
Parameter | Type | Description |
---|---|---|
to | String | Recipient(s) 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's email |
cc | String | Email addresses to be carbon-copied, separated by semicolons |
filesToAttach | Collection | Attachment names |
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 have access to an SMTP server. SMTP is the "outgoing" part of email. If you are using a virtual host, you might already know the name of your SMTP server. If you are on a company network, your IT department will provide you with a name. If you are working from home, you might be able to use your regular email service provider.
To send an email, you will need:
- The name of the SMTP server
- The port number (usually 25)
- The username for the email
- The password for the email
In your web root directory, create a page named _AppStart.cshtml (if it already exists, just edit the page).
Copy the following code into the file:
_AppStart.cshtml
The above code will run every time the site (application) starts. It initializes the WebMail object with default values.
Please replace:
Replace smtp.example.com with the name of the SMTP server you will use to send emails.
Replace 25 with the port number used by the server to send SMTP transactions (emails).
If the server uses SSL (Secure Socket Layer) encryption, replace false with true.
Replace [email protected] with the name of the SMTP email account used to send emails.
Replace password with the password for the SMTP email account.
Replace john@example with the email displayed in the sender's address field.
| | In your AppStart file, you do not need to initialize the WebMail object, but you must set these properties before calling the WebMail.Send() method. | | --- | --- |