ASP.NET Tutoria...login
ASP.NET Tutorial
author:php.cn  update time:2022-04-11 14:18:18

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

PropertiesDescription
SmtpServerThe name of the SMTP server used to send emails.
SmtpPortThe port used by the server to send SMTP emails.
EnableSslValue is true if the server uses SSL (Secure Socket Layer) encryption.
UserNameThe name of the SMTP email account used to send emails.
PasswordPassword for the SMTP email account.
FromThe email displayed in the From address field (usually the same as UserName).


WebMail Object Reference Manual - Methods

MethodDescription
Send()Send the email message that needs to be sent to the SMTP server.

The Send() method has the following parameters:

ParametersTypeDescription
toStringRecipients (separated by semicolon)
subjectStringEmail subject
bodyStringEmail body

Send() method has the following optional parameters:

ParametersTypeDescription
fromString Sender
ccStringEmail addresses to be copied (separated by semicolons)
filesToAttachCollectionAttachment name
isBodyHtmlBooleanTrue if the email body is in HTML format
additionalHeadersCollectionAdditional Title


Technical Data

NameValue
Class System.Web.Helpers.WebMail
NamespaceSystem.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.

lamp.gifIn 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.


php.cn