This section explains
Properties, methods of the WebMail object, and how to initialize the WebMail helper.The WebMail object allows you to easily send e-mail messages from a web page.
The WebMail object provides ASP.NET Web Pages with the functionality to send emails using SMTP (Simple Mail Transfer Protocol).
See the WebPages Email chapter for an example.
property | describe |
---|---|
SmtpServer | The name of the SMTP server used to send email. |
SmtpPort | The port the server uses to send SMTP 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 | Password for the SMTP email account. |
From | The email message that appears in the From address field (usually the same as UserName). |
method | describe |
---|---|
Send() | Send the email message that needs to be delivered to the SMTP server. |
The Send() method has the following parameters:
parameter | type | describe |
---|---|---|
to | String | Recipients (separated by semicolon) |
subject | String | Email subject |
body | String | Email text |
The Send() method has the following optional parameters:
parameter | type | describe |
---|---|---|
from | String | sender |
cc | String | Email addresses to be copied (separated by semicolon) |
filesToAttach | Collection | Attachment name |
isBodyHtml | Boolean | true if the email body is in HTML format |
additionalHeaders | Collection | additional title |
name | value |
---|---|
Class | System.Web.Helpers.WebMail |
Namespace | System.Web.Helpers |
Assembly | System.Web.Helpers.dll |
To use the WebMail Helper, you must have access to an SMTP server. SMTP is the "output" part of email. If you are using virtual 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:
SMTP server name
Port number (usually 25)
Email username
Email password
In your web root directory, create a page called _AppStart.cshtml (or edit the page directly if it already exists).
Copy the following code into the file:
@{ WebMail.SmtpServer = "smtp.example.com"; WebMail.SmtpPort = 25; WebMail.EnableSsl = false; WebMail.UserName = "[email protected]"; WebMail.Password = "password"; WebMail.From = "[email protected]" }
The above code will run every time the website (application) starts. 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 the server uses to send SMTP transactions (email).
If the server uses SSL (Secure Socket Layer) encryption, replace false with true.
Replace [email protected] with the name of the SMTP email account you use 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 do not need to start the WebMail object , but you must set these properties before calling the WebMail.Send() method. |