课程表

WebPages 教程

WebPages 参考手册

ASP.net Razor

ASP.net MVC

WebForms 教程

WebForms 参考手册

工具箱
速查手册

WebPages 电邮

当前位置:免费教程 » 程序设计 » ASP.net

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

WebMail 帮助器

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

脚本: Email 支持

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

首先:编辑您的 AppStart 页面

如果您曾构建过本教程中的 DEMO 应用程序,那么站点中应该存在拥有如下内容的 _AppStart.cshtml 页面:

_AppStart.cshtml

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

如需初始化 WebMail 帮助器,请向您的 AppStart 页面添加以下 WebMail 属性:

_AppStart.cshtml

  1. @{
  2. WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email",
  3. true);
  4. WebMail.SmtpServer = "smtp.example.com";
  5. WebMail.SmtpPort = 25;
  6. WebMail.EnableSsl = false;
  7. WebMail.UserName = "support@example.com";
  8. WebMail.Password = "password-goes-here";
  9. WebMail.From = "john@example.com";
  10. }

属性解释:

SmtpServer: 发送电邮所使用的 SMTP 服务器的名称。

SmtpPort: 发送 SMTP transactions (电邮) 所用的服务器端口。

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

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

Password: SMTP 电邮账户的密码。

From: 出现在 from 栏中的电邮地址(通常与 UserName 相同)。

第二:创建电邮输入页面

然后创建输入页面,名为 Email_Input:

Email_Input.cshtml

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>Request for Assistance</h1>
  5.  
  6. <form method="post" action="EmailSend.cshtml">
  7. <label>Username:</label>
  8. <input type="text name="customerEmail" />
  9. <label>Details about the problem:</label>
  10. <textarea name="customerRequest" cols="45" rows="4"></textarea>
  11. <p><input type="submit" value="Submit" /></p>
  12. </form>
  13.  
  14. </body>
  15. </html>

输入页面的作用是收集信息,然后把数据提交到一个能够将信息作为邮件来发送的新页面。

第三:创建邮件发送页面

然后创建用于发送电邮的页面,名为 Email_Send:

Email_Send.cshtml

  1. @{ // Read input
  2. var customerEmail = Request["customerEmail"];
  3. var customerRequest = Request["customerRequest"];
  4. try
  5. {
  6. // Send email
  7. WebMail.Send(to:"someone@example.com",
  8. subject: "Help request from - " + customerEmail,
  9. body: customerRequest );
  10. }
  11. catch (Exception ex )
  12. {
  13. <text>@ex</text>
  14. }
  15. }

如需更多有关从 ASP.NET Web Pages 应用程序发送电子邮件的信息,请参阅:WebMail 对象参考手册

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号