ASP.NET MVC 3 Beta初体验之实用的WebMail【附示例下载】

Asp.net MVC 3 Beta中提供了非常实用发送邮件的组件:WebMail。我试用了一下,和System.Web.Mail类似。这篇文章将简单介绍一下这个组件的使用。通过分成不带附件的邮件发送和带附件的邮件发送两种情况进行讲解。用一个请求帮助的应用场景为例。完整示例下载

不带附件的邮件发送

首先定义Controller。EmailRequest用于请求一个发送邮件的页面,ProcessRequest用去处理发送邮件的请求,并在View中发送邮件。

  1. [HttpGet] 
  2. public ActionResult EmailRequest()  
  3. {  
  4.     return View();  
  5.  
  6. [HttpPost] 
  7. public ActionResult ProcessRequest() 
  8.     return View(); 

EmailRequest.cshtml代码如下:

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head>     
  4. <title>求助中心</title></head><body>   
  5. <h2>发送邮件求助</h2>   
  6. <form method="post" action="ProcessRequest">     
  7. <div>你的姓名:          
  8. <input type="text" name="customerName" />     
  9. </div>      
  10. <div>你的问题描述: <br />         
  11. <textarea name="customerRequest" cols="45" rows="4"> 
  12. </textarea> 
  13. </div>      
  14. <div>         
  15. <input type="submit" value="Submit" />     
  16. </div>   
  17. </form> 
  18. </body> 
  19. </html>  

发送邮件的View:

  1. @{     
  2. var customerName = Request["customerName"];     
  3. var customerRequest = Request["customerRequest"];     
  4. try  
  5. {         
  6.     // 初始化        
  7.     WebMail.SmtpServer = "smtp.126.com";         
  8.     WebMail.SmtpPort = 25;         
  9.     WebMail.EnableSsl = false;         
  10.     WebMail.UserName = "zhuqi0";         
  11.     WebMail.From = "zhuqi0@126.com";         
  12.     WebMail.Password = "**********";                  
  13.     // 发送邮件        
  14.     WebMail.Send(to:"zhuqi0@126.com",              
  15.     subject: "来自 - " + customerName+"的求助",             
  16.     body: customerRequest         
  17.     );     
  18. }     
  19. catch (Exception ex )  
  20. {         
  21.     <text>           
  22.     <b>邮件发送<em>失败</em></b>            
  23.       代码中没有提供正确的SMTP服务名,用户名,密码等信息。        
  24.     </text>     
  25. }  
  26. <!DOCTYPE html> 
  27. <html><head>   
  28. <title>求助中心</title></head><body>   
  29. <p>非常抱歉听到你有麻烦,  
  30. <b>@customerName</b>
  31. </p>    
  32. <p>关于下面问题的邮件已经发送给我们的客服,相关部门会及时处理。</p>    
  33. <p><b>@customerRequest</b></p></body></html>  

运行:

发送成功页面

邮件通知:

带附件的邮件发送:

带附件的邮件发送类似,不过需要知道附加地址的列表,发送邮件的带附件的邮件代码如下:

  1. @{     
  2. var customerName = Request["customerName"];     
  3. var subjectLine = Request["subjectLine"];     
  4. var fileAttachment = Request["fileAttachment"];      
  5. try {         
  6. // 初始化       
  7. WebMail.SmtpServer = "smtp.126.com";         
  8. WebMail.SmtpPort = 25;         
  9. WebMail.EnableSsl = false;         
  10. WebMail.UserName = "zhuqi0";         
  11. WebMail.From = "zhuqi0@126.com";         
  12. WebMail.Password = "**********";                  
  13. // 创建包含附件的数组       
  14. var filesList = new string [] { fileAttachment };                 
  15.  // 添加附件和发送邮件        
  16. WebMail.Send(to: "zhuqi0@126.com",subject: subjectLine,             
  17. body: "File attached. <br />From: " + customerName,  
  18. filesToAttach: filesList);     
  19. }     
  20. catch (Exception ex)  
  21. {         
  22.     <text>           
  23.     <b>邮件发送<em>失败</em></b>            
  24.       代码中没有提供正确的SMTP服务名,用户名,密码等信息。        
  25.     </text>     
  26. }  
  27. <!DOCTYPE html> 
  28. <html> 
  29. <head>     
  30. <title>求助中心</title> 
  31. </head> 
  32. <body>  
  33.  <p><b>@customerName</b>, 感谢你的支持.</p>   <p>关于下面问题的邮件已经发送给我们的客服,相关部门会及时处理。 <b> 
  34. @fileAttachment</b> 
  35.  file attached.</p> 
  36.  </body> 
  37. </html>  

从上面的两种情况我们可以看到,WebMail和System.Web.Mail使用的方式是一样的,不过在Asp.net MVC 3 Beta中WebMail使用起来更简便了。

第一步:初始化,指定邮件发送服务器。
WebMail.SmtpServer = "smtp.126.com";

第二步:指定端口。
WebMail.EnableSsl = false;

第三步:指定用户名。
WebMail.UserName = "zhuqi0";

第四步:你的邮箱地址和密码。
WebMail.From = "zhuqi0@126.com";
WebMail.Password = "********";

第五步:如果有附件指定附件地址。
var filesList = new string [] { fileAttachment };

第六步:邮件发送。
WebMail.Send(to: "zhuqi0@126.com",subject: subjectLine,
body: "File attached. <br />From: " + customerName,
filesToAttach: filesList);

总结:本文简单介绍了一下ASP.NET MVC 3 Beta中WebMail的使用。   

转自:http://www.aspxcs.net/HTML/0930152765.html

原文地址:https://www.cnblogs.com/stalwart/p/1878528.html