ASP.NET WP - 添加电子邮件

在本章中,我们将介绍如何向网站添加电子邮件以及如何从网页发送电子邮件.您可能需要从您的网站发送电子邮件的原因有很多.

  • 您可以向用户发送确认消息.

  • 您也可以向自己发送通知.例如,当新用户在网站上注册时.

使用 WebMail发送电子邮件非常简单辅助的.要使用此WebMail帮助程序,您必须能够访问SMTP(SMTP代表简单邮件传输协议)服务器.

  • SMTP服务器是一种电子邮件服务器,仅将邮件转发给收件人的服务器.

  • 如果您为您的网站使用托管服务提供商,那么他们将设置您的电子邮件,他们可以告诉你你的SMTP服务器名称是什么.

  • 如果你在公司网络内工作,管理员或你的IT部门通常可以给您可以使用有关SMTP服务器的信息.

  • 如果您在家工作,您甚至可以使用普通电子邮件提供商进行测试,谁可以告诉你他们的SMTP服务器的名称.

要使用SMTP服务器,您需要以下内容;

  • SMTP服务器的名称.

  • 端口数量大多是25.但是,您的ISP可能会要求您使用por t 587.

  • 凭证,例如用户名,密码.

<让我们看一个简单的例子,我们将在其中发送电子邮件.首先,我们需要创建一个新的CSHTML文件.文件类型

在"名称"字段中输入 EmailRequest.cshtml ,然后单击"确定".

现在替换EmailRequest.cshtml文件中的以下代码.

<!DOCTYPE html>
<html>
   
   <head>
      <title>Request for Assistance</title>
   </head>
   
   <body>
      <h2>Submit Email Request for Assistance</h2>
      <form method = "post" action = "ProcessRequest.cshtml">
         <div>
            Your name:
            <input type = "text" name = "customerName" />
         </div>
         
         <div>
            Your email address:
            <input type = "text" name = "customerEmail" />
         </div>
         
         <div>
            Details about your problem: <br />
            <textarea name = "customerRequest" cols = "45" rows = "4"></textarea>
         </div>
         
         <div>
            <input type = "submit" value = "Submit" />
         </div>
      </form>
   
   </body>
</html>


正如您在上面的代码中看到的那样,表单的action属性设置为 ProcessRequest.cshtml ,这意味着表格将提交到该页面.因此,让我们创建另一个CSHTML文件ProcessRequest.cshtml并替换以下代码.

@{
   var customerName = Request["customerName"];
   var customerEmail = Request["customerEmail"];
   var customerRequest = Request["customerRequest"];
   var errorMessage = "";
   var debuggingFlag = false;
   
   try {
      // Initialize WebMail helper
      WebMail.SmtpServer = "smtp.mail.yahoo.com";
      WebMail.SmtpPort = 465;
      WebMail.UserName = "waqasm78@yahoo.com";
      WebMail.Password = "**********";
      WebMail.From = "waqasm78@yahoo.com";
      
      // Send email
      WebMail.Send(to: customerEmail,
         subject: "Help request from - " + customerName,
         body: customerRequest
      );
   }catch (Exception ex ) {
      errorMessage = ex.Message;
   }
}

<!DOCTYPE html>
<html>
   
   <head>
      <title>Request for Assistance</title>
   </head>
   
   <body>
      <p>Sorry to hear that you are having trouble, <b>@customerName</b>.</p>
      
      @if(errorMessage == ""){
         <p>An email message has been sent to our customer service department regarding 
            the following problem:</p>
         <p><b>@customerRequest</b></p>
      } else{
         <p><b>The email was <em>not</em> sent.</b></p>
         <p>Please check that the code in the ProcessRequest page has 
            correct settings for the SMTP server name, a user name, 
            a password, and a "from" address.</p>
         
         if(debuggingFlag){
            <p>The following error was reported:</p>
            <p><em>@errorMessage</em></p>
         }
      }
   
   </body>
</html>


如果您使用的是Yahoo电子邮件提供商,那么您必须在上述程序中替换以下代码才能使其运行.

// Initialize WebMail helper
   WebMail.SmtpServer = "smtp.mail.yahoo.com";
   WebMail.SmtpPort = 465;
   WebMail.UserName = "waqasm78@yahoo.com";
   WebMail.Password = "**********";
   WebMail.From = "waqasm78@yahoo.com";

您需要在 WebMail.Password 属性中输入您自己的密码.

现在让我们运行应用程序并指定以下url :   http://localhost:59795/EmailRequest ,您将看到以下网页.

提交电子邮件

现在在所有提到的字段中输入一些信息,如以下屏幕截图所示.

电子邮件请求

点击提交,然后您会在成功发送电子邮件时看到以下消息.

请求帮助