Latest web development tutorials

ASP.NET Web Pages WebMail Reference Manual

By WebMail object, you can easily send e-mail from a Web page.


description

WebMail object ASP.NET Web Pages provides methods of using SMTP (Simple Mail Transfer Protocol Simple Mail Transfer Protocol) to send e-mail function.


Examples

See the WebPages Email chapter instance.


WebMail Object Reference - Property

属性 描述
SmtpServer 用于发送电子邮件的 SMTP 服务器的名称。
SmtpPort 服务器用来发送 SMTP 电子邮件的端口。
EnableSsl 如果服务器使用 SSL(Secure Socket Layer 安全套接层)加密,则值为 true。
UserName 用于发送电子邮件的 SMTP 电子邮件账户的名称。
Password SMTP 电子邮件账户的密码。
From 在发件地址栏显示的电子邮件(通常与 UserName 相同)。


WebMail Object Reference - Methods

方法 描述
Send() 向 SMTP 服务器发送需要传送的电子邮件信息。

Send () method has the following parameters:

参数 类型 描述
to String 收件人(用分号分隔)
subject String 邮件主题
body String 邮件正文

Send () method has the following optional parameters:

参数 类型 描述
from String 发件人
cc String 需要抄送的电子邮件地址(用分号分隔)
filesToAttach Collection 附件名
isBodyHtml Boolean 如果邮件正文是 HTML 格式的,则为 true
additionalHeaders Collection 附加的标题


Technical data

名称
Class System.Web.Helpers.WebMail
Namespace System.Web.Helpers
Assembly System.Web.Helpers.dll


Initialize the WebMail helper

To use the WebMail helper, you must have access to an SMTP server. SMTP is the "output" section of the e-mail. If you are using a virtual host, you may already know the name of the SMTP server. If you're using a corporate network to work, your company's IT department will give you a name. If you are working at home, you may be able to use ordinary e-mail service provider.

To send an email, you will need:

  • SMTP server name
  • Port number (typically 25)
  • Username email
  • Email password

In your Web root directory, create a file called _AppStart.cshtml page (if it already exists, you will directly edit page).

Copy the following code to 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 run at each site (application) starts. It WebMail objects assigned an initial value.

Replace:

Smtp.example.com will replace a name that you want to use to send e-mail SMTP server.

25 will replace the server used to send SMTP transaction (e-mail) port number.

If the server uses SSL (Secure Socket Layer Secure Sockets Layer) encryption, set false replace true.

Will replace support@example.com a name used for sending e-mail SMTP e-mail accounts.

Will replace the password into the password SMTP e-mail accounts.

Will replace john @ example to appear in the e-mail sender's address bar.

lamp AppStart in your file, you need to start WebMail object, but before calling WebMail.Send () method, you must set these properties.