ASP.NET WebMail 网页电子邮件

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

使用 WebMail 对象,您可以轻松地从网页发送电子邮件。


WebMail 助手

WebMail 助手使我们更容易从 web 应用程序中使用 SMTP 来发送电邮。


脚本: Email 支持

为了演示电子邮件的使用,我们将创建用于技术支持的输入页面,让用户向另一个页面提交该页面,然后发送一封有关支持问题的电子邮件。


首先:编辑您的 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 transactions (电邮) 所用的服务器端口。

EnableSsl: True,如果服务器应该使用 SSL (Secure Socket Layer) 加密。

UserName: 发送电邮所用的 SMTP email 账户的名称。

Password: SMTP 电邮账户的密码。

From: 出现在 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>
}
}

WebMail 对象参考 - 属性

属性 描述
SmtpServer 将发送电子邮件的 SMTP 服务器的名称
SmtpPort 服务器将用于发送 SMTP 电子邮件的端口
EnableSsl 如果服务器应该使用 SSL 加密,则为真
UserName 用于发送电子邮件的 SMTP 帐户的名称
Password SMTP账号的密码
From 显示在发件人地址中的电子邮件

WebMail 对象参考 - 方法

方法 描述
Send() 将电子邮件消息发送到 SMTP 服务器以进行传递

Send() 方法有以下参数:

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

以及以下可选参数:

参数 类型 描述
from String 发件人的邮箱
cc String 抄送邮件(分号隔开)
filesToAttach Collection 文件名
isBodyHtml Boolean 如果电子邮件正文是 HTML,则为真
additionalHeaders Collection 附加标题

技术细节

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

初始化 WebMail 帮助程序

要使用 WebMail 帮助程序,您需要访问 SMTP 服务器。 SMTP 是电子邮件的 "output" 部分。 如果您使用网络主机,您可能已经知道 SMTP 服务器的名称。 如果您在公司网络中工作,您的 IT 部门可以为您提供名称。 如果您在家工作,您也许可以使用您的普通电子邮件提供商。

要发送电子邮件,您需要:

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

在您网站的根目录中,创建一个名为 _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 事务(电子邮件)的端口号。

false 为 true,如果服务器应使用 SSL(安全套接层)加密。

support@example.com 带有用于发送电子邮件的 SMTP 电子邮件帐户的名称。

password 为 SMTP 电子邮件帐户的密码。

john@example 带有要显示在发件人地址中的电子邮件。

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