Web Page Post Office
ASP.NET Web Pages - WebMail Object
The WebMail object allows you to easily send email from a web page.
Description
The WebMail object Provides ASP.NET Web Pages with the functionality to send emails using SMTP (Simple Mail Transfer Protocol).
Examples
Please see the examples in the WebPages Email chapter.
WebMail Object Reference Manual - Properties
Properties | Description |
---|---|
SmtpServer | The name of the SMTP server used to send emails. |
SmtpPort | The port used by the server to send SMTP emails. |
EnableSsl | 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 | Password for the SMTP email account. |
From | The email displayed in the From address field (usually the same as UserName). |
WebMail Object Reference Manual - Methods
Method | Description |
---|---|
Send() | Send the email message that needs to be sent to the SMTP server. |
The Send() method has the following parameters:
Parameters | Type | Description |
---|---|---|
to | String | Recipients (separated by semicolon) |
subject | String | Email subject |
body | String | Email body |
Send() method has the following optional parameters:
Parameters | Type | Description |
---|---|---|
from | String | Sender |
cc | String | Email addresses to be copied (separated by semicolons) |
filesToAttach | Collection | Attachment name |
isBodyHtml | Boolean | True if the email body is in HTML format |
additionalHeaders | Collection | Additional Title |
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 Access the SMTP server. SMTP is the "output" part of email. If you are using web hosting, you probably already know the name of your SMTP server. If you work on 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.
In order to send an email you will need:
The name of the SMTP server
The port number (usually 25)
Email username
Email password
in your In the 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 = "support@example.com";
WebMail.Password = "password";
WebMail.From = "john@example.com"
}
The above code will be used every time the website (application) Run on startup. It assigns an initial value to the WebMail object.
Please replace:
Replace smtp.example.com with the name of the SMTP server you want to use to send email.
Replace 25 with the port number that the server uses to send SMTP transactions (email).
If the server uses SSL (Secure Socket Layer) encryption, please replace false with true.
Replace support@example.com with the name of the SMTP email account used to send the email.
Replace password with the password for your SMTP email account.
Replace john@example with the email that appears in the From address field.
In your AppStart file, you don't need to start the WebMail object, but when calling You must set these properties before calling the WebMail.Send() method. |