ASP.NET Web Pages - WebMail Object

Using the WebMail object, you can easily send emails from a web page.

Description

WebMail provides methods for building and sending emails using the Simple Mail Transfer Protocol (SMTP).

Example

Refer to Web Pages Mail The example in this chapter.

WebMail Object Reference Manual - Property

Property Description
EnableSsl True if the server uses SSL encryption.
From Gets or sets the sender's email address.
Password Gets or sets the password for the sender's email account.
SmtpPort Gets or sets the port used for SMTP transactions.
SmtpServer Gets or sets the name of the SMTP server used to transmit emails.
UserName Gets or sets the email account name used to send emails.

WebMail Reference Manual - Method

Method Description
Send() Sends the specified email to the SMTP server for delivery.

The Send() method has the following parameters:

Parameter Type Description
to String The recipient's email address. Separated by a semicolon (;).
subject String The subject line.
body String The body of the email.

and the following optional parameters:

Parameter Type Description
from String The sender's email address.
cc String The addresses to which the email is copied; separated by a semicolon (;).
filesToAttach Collection A collection of filenames used to specify the files to be attached to the email.
isBodyHtml Logical value If true, specifies that the email body is in HTML format.
additionalHeaders Collection A collection of headers that can be added to the normal SMTP headers included in this email.
bcc String The email addresses of other recipients to whom the 'blind carbon copy' copy of the email will be sent.
contentEncoding String The encoding used for the email body.
headerEncoding String The encoding used for the email header.
priority String The value used to specify the email priority.
replyTo String The email address that will be used when the recipient replies to the email.

Technical data

Name Value
Class System.Web.Helpers.WebMail
Namespace System.Web.Helpers
Assembly System.Web.Helpers.dll

Initialize WebMail Assistant

To use the WebMail Assistant, you need to access an SMTP server. SMTP is the 'output' part of email. If you use a web host, you may have already obtained the name of the SMTP server. If you are in a corporate network, you will need to find out the name of the SMTP server from the IT department. If you work from home, you can use a regular email provider.

To send emails, you need:

  • SMTP Server Name
  • Port Number (usually 25)
  • Email Username
  • Email Password

In the root directory of the website, create (or edit) a page named _AppStart.cshtml:

Enter the following code into this 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 runs each time the website (application) starts. It will set WebMail objectto initialize.

Please replace:

smtp.example.com For the name of the SMTP server used to send emails.

25 For the port number that the server will use to handle SMTP transactions (emails).

false For true if the server uses Secure Sockets Layer (SSL) to encrypt the connection when sending emails.

support@example.com For the name of the SMTP email account used to send emails.

password For the password of the SMTP email account.

john@example For the sender's address.

Tip:You are not required to initialize the WebMail object in the AppStart file, but you must set these properties before calling the WebMail.Send() method.