非常教程

ASP.NET 教程教程

ASP.NET Web Pages WebMail 参考手册

ASP.NET Web Pages WebMail 参考手册

ASP.NET Web Pages - WebMail 对象


通过 WebMail 对象,您可以很容易地从网页上发送电子邮件。


描述

WebMail 对象为 ASP.NET Web Pages 提供了使用 SMTP(Simple Mail Transfer Protocol 简单邮件传输协议)发送邮件的功能。


实例

请查看 WebPages Email 章节中的实例。


WebMail 对象参考手册 - 属性

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


WebMail 对象参考手册 - 方法

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

Send() 方法有以下参数:

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

Send() 方法有以下可选参数:

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


技术数据

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


初始化 WebMail 帮助器

要使用 WebMail 帮助器,您必须能访问 SMTP 服务器。SMTP 是电子邮件的"输出"部分。如果您使用的是虚拟主机,您可能已经知道 SMTP 服务器的名称。如果您使用的是公司网络工作,您公司的 IT 部门会给您一个名称。如果您是在家工作,你也许可以使用普通的电子邮件服务提供商。

为了发送一封电子邮件,您将需要:

  • SMTP 服务器的名称
  • 端口号(通常是 25 )
  • 电子邮件的用户名
  • 电子邮件的密码

在您的 Web 根目录下,创建一个名为 _AppStart.cshtml 的页面(如果已存在,则直接编辑页面)。

将下面的代码复制到文件中:

_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"
}

上面的代码将在每次网站(应用程序)启动时运行。它对 WebMail 对象赋了初始值。

请替换:

smtp.example.com 替换成您要用来发送电子邮件的 SMTP 服务器的名称。

25 替换成服务器用来发送 SMTP 事务(电子邮件)的端口号。

如果服务器使用 SSL(Secure Socket Layer 安全套接层)加密,请将 false 替换成 true。

support@example.com 替换成用来发送电子邮件的 SMTP 电子邮件账户的名称。

password 替换成 SMTP 电子邮件账户的密码。

john@example 替换成显示在发件地址栏中的电子邮件。

ASP.NET Web Pages WebMail 参考手册

在您的 AppStart 文件中,您不需要启动 WebMail 对象,但是在调用 WebMail.Send() 方法之前,您必须设置这些属性。


ASP.NET Web Pages WebMail 参考手册
ASP.NET 教程

ASP.NET 是微软公司提出的一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。ASP.NET 支持三种不同的开发模式:单页面模式、MVC 模式、事件驱动模式。

ASP.NET 教程目录

1.ASP.NET Web Pages Razor
2.ASP.NET Web Pages 教程
3.ASP.NET
4.ASP.NET 教程
5.ASP.NET Web Pages 布局
6.ASP.NET Web Pages 对象
7.ASP.NET Web Pages HTML 表单
8.ASP.NET Web Pages – 发布
9.ASP.NET Web Pages Email
10.ASP.NET Web Pages WebGrid
11.ASP.NET Web Pages 文件
12.ASP.NET Razor 语法
13.ASP.NET Razor 标记
14.ASP.NET WebPages 帮助器参考手册
15.ASP.NET Web Pages WebMail 参考手册
16.ASP.NET Web Pages Database 参考手册
17.ASP.NET Web Pages WebSecurity 参考手册
18.ASP.NET Web Pages 类参考手册
19.ASP.NET Web 的 C# 和 VB 实例
20.ASP.NET Razor C# 循环和数组
21.ASP.NET Razor C# 变量
22.ASP.NET Razor VB 逻辑
23.ASP.NET Razor VB 循环和数组
24.ASP.NET Razor VB 变量
25.ASP.NET Razor C# 逻辑
26.ASP.NET MVC 模型
27.ASP.NET MVC 页面和布局
28.ASP.NET 事件句柄
29.ASP.NET 服务器控件
30.ASP.NET Web 页面
31.ASP.NET Web Forms 教程
32.ASP.NET MVC 参考手册
33.ASP.NET MVC – 发布
34.ASP.NET MVC HTML 帮助器
35.ASP.NET Repeater 控件
36.ASP.NET XML 数据绑定
37.ASP.NET SortedList
38.ASP.NET Hashtable
39.ASP.NET ArrayList
40.ASP.NET 数据绑定
41.ASP.NET Button 控件
42.ASP.NET TextBox 控件
43.ASP.NET ViewState
44.ASP.NET Web 表单
45.ASP.NET DataList 控件
46.ASP.NET 母版页
47.ASP.NET 数据库连接
48.ASP.NET 实例
49.ASP.NET 导航
50.ASP.NET HtmlAnchor 控件
51.ASP.NET HtmlGeneric 控件
52.ASP.NET HtmlForm 控件
53.ASP.NET HtmlButton 控件
54.ASP.NET HtmlInputHidden 控件
55.ASP.NET HtmlInputFile 控件
56.ASP.NET HtmlInputCheckBox 控件
57.ASP.NET HtmlInputButton 控件
58.ASP.NET HtmlImage 控件
59.ASP.NET AdRotator 控件
60.ASP.NET HTML 服务器控件
61.ASP.NET HtmlTextArea 控件
62.ASP.NET HtmlTableRow 控件
63.ASP.NET HtmlTableCell 控件
64.ASP.NET HtmlTable 控件
65.ASP.NET HtmlSelect 控件
66.ASP.NET HtmlInputText 控件
67.ASP.NET HtmlInputRadioButton 控件
68.ASP.NET HtmlInputImage 控件
69.ASP.NET Label 控件
70.ASP.NET ImageButton 控件
71.ASP.NET Image 控件
72.ASP.NET HyperLink 控件
73.ASP.NET DropDownList 控件
74.ASP.NET CheckBoxList 控件
75.ASP.NET CheckBox 控件
76.ASP.NET CalendarDay 控件
77.ASP.NET Calendar 控件
78.ASP.NET Button 控件
79.ASP.NET Style 控件
80.ASP.NET BulletedList 控件
81.ASP.NET RadioButtonList 控件
82.ASP.NET RadioButton 控件
83.ASP.NET PlaceHolder 控件
84.ASP.NET Panel 控件
85.ASP.NET Literal 控件
86.ASP.NET ListItem 控件
87.ASP.NET ListBox 控件
88.ASP.NET LinkButton 控件
89.ASP.NET RegularExpressionValidator 控件
90.ASP.NET RangeValidator 控件
91.ASP.NET CustomValidator 控件
92.ASP.NET CompareValidator 控件
93.ASP.NET Web 服务器控件
94.ASP.NET XML 控件
95.ASP.NET TextBox 控件
96.ASP.NET TableRow 控件
97.ASP.NET TableCell 控件
98.ASP.NET Table 控件
99.ASP.NET Validation 服务器控件
100.ASP.NET ValidationSummary 控件