×
ASP.NET 教程ASP.NET 简介

WP 教程

WebPages 简介WebPages RazorWebPages 布局WebPages 文件夹WebPages 全局WebPages 表单WebPages 对象WebPages 文件WebPages 帮助器WebPages WebGridWebPages 图表WebPages EmailWebPages PHPWebPages 发布WebPages 实例

WP 参考手册

WebPages 类WebPages 安全WebPages 数据库WebPages 邮局WebPages 帮助器

ASP.NET Razor

Razor 简介Razor 语法Razor C# 变量Razor C# 循环Razor C# 逻辑Razor VB 变量Razor VB 循环Razor VB 逻辑

ASP.NET MVC

MVC 简介MVC 应用程序MVC 文件夹MVC 布局MVC 控制器MVC 视图MVC 数据库MVC 模型MVC 安全MVC HTML 帮助器MVC 发布MVC 参考手册

WF 教程

WebForms 简介WebForms 页面WebForms 控件WebForms 事件WebForms 表单WebForms ViewStateWebForms TextBoxWebForms ButtonWebForms 数据绑定WebForms ArrayListWebForms HashtableWebForms SortedListWebForms XML 文件WebForms RepeaterWebForms DataListWebForms 数据库连接WebForms 母版页WebForms 导航WebForms 实例

WF 参考手册

WebForms HTMLWebForms ControlsWebForms Validation

ASP.NET Web Pages Email


WebMail 帮助器 - 众多有用的 ASP.NET Web 帮助器之一。


WebMail 帮助器

WebMail 帮助器让发送邮件变得更简单,它按照 SMTP(Simple Mail Transfer Protocol 简单邮件传输协议)从 Web 应用程序发送邮件。


前提:电子邮件支持

为了演示如何使用电子邮件,我们将创建一个输入页面,让用户提交一个页面到另一个页面,并发送一封关于支持问题的邮件。


第一:编辑您的 AppStart 页面

如果在本教程中您已经创建了 Demo 应用程序,那么您已经有一个名为 _AppStart.cshtml 的页面,内容如下:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}

要启动 WebMail 帮助器,向您的 AppStart 页面中增加如下所示的 WebMail 属性:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "support@example.com";
WebMail.Password = "password-goes-here";
WebMail.From = "john@example.com";

}

属性解释:

SmtpServer: 用于发送电子邮件的 SMTP 服务器的名称。

SmtpPort: 服务器用来发送 SMTP 事务(电子邮件)的端口。

EnableSsl: 如果服务器使用 SSL(Secure Socket Layer 安全套接层)加密,则值为 true。

UserName: 用于发送电子邮件的 SMTP 电子邮件账户的名称。

Password: SMTP 电子邮件账户的密码。

From: 在发件地址栏显示的电子邮件(通常与 UserName 相同)。


第二:创建一个电子邮件输入页面

接着创建一个输入页面,并将它命名为 Email_Input:

Email_Input.cshtml

<!DOCTYPE html>
<html>
<body>
<h1>Request for Assistance</h1>

<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest" cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit" /></p>
</form>

</body>
</html>

输入页面的目的是手机信息,然后提交数据到可以将信息作为电子邮件发送的一个新的页面。


第三:创建一个电子邮件发送页面

接着创建一个用来发送电子邮件的页面,并将它命名为 Email_Send:

Email_Send.cshtml

@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"someone@example.com", subject: "Help request from - " + customerEmail, body: customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}

想了解更多关于 ASP.NET Web Pages 应用程序发送电子邮件的信息,请查阅:WebMail 对象参考手册


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)