ASP.NET MVC 3 Beta初体验之实用的WebMail

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

不带附件的邮件发送

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

ASP.NET MVC 3 Beta初体验之实用的WebMail
ASP.NET MVC 3 Beta初体验之实用的WebMail代码
[HttpGet]
public ActionResult EmailRequest() 

return View(); 
}

[HttpPost]
public ActionResult ProcessRequest()
{
return View();
}
ASP.NET MVC 3 Beta初体验之实用的WebMail

EmailRequest.cshtml代码如下:

ASP.NET MVC 3 Beta初体验之实用的WebMail
ASP.NET MVC 3 Beta初体验之实用的WebMail代码
<!DOCTYPE html>
<html>
<head> 
<title>求助中心</title></head><body> 
<h2>发送邮件求助</h2> 
<form method="post" action="ProcessRequest"> 
<div>你的姓名: 
<input type="text" name="customerName"/> 
</div> 
<div>你的问题描述: <br /> 
<textarea name="customerRequest" cols="45" rows="4">
</textarea>
</div> 
<div> 
<input type="submit" value="Submit"/> 
</div> 
</form>
</body>
</html>
ASP.NET MVC 3 Beta初体验之实用的WebMail

 发送邮件的View:

ASP.NET MVC 3 Beta初体验之实用的WebMail
@{ 
var customerName = Request["customerName"]; 
var customerRequest = Request["customerRequest"]; 
try 

// 初始化 
WebMail.SmtpServer = "smtp.126.com"; 
WebMail.SmtpPort = 25; 
WebMail.EnableSsl = false; 
WebMail.UserName = "zhuqi0"; 
WebMail.From = "[email protected]"; 
WebMail.Password = "**********"; 
// 发送邮件 
WebMail.Send(to:"[email protected]", 
subject: "来自 - " + customerName+"的求助", 
body: customerRequest 
); 

catch (Exception ex ) 

<text> 
<b>邮件发送<em>失败</em></b> 
代码中没有提供正确的SMTP服务名,用户名,密码等信息。 
</text> 
}

<!DOCTYPE html>
<html><head> 
<title>求助中心</title></head><body> 
<p>非常抱歉听到你有麻烦, 
<b>@customerName</b>.
</p> 
<p>关于下面问题的邮件已经发送给我们的客服,相关部门会及时处理。</p> 
<p><b>@customerRequest</b></p></body></html> 
ASP.NET MVC 3 Beta初体验之实用的WebMail

运行:ASP.NET MVC 3 Beta初体验之实用的WebMail

发送成功页面

ASP.NET MVC 3 Beta初体验之实用的WebMail

邮件通知:

ASP.NET MVC 3 Beta初体验之实用的WebMail

带附件的邮件发送:

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

ASP.NET MVC 3 Beta初体验之实用的WebMail
@{ 
var customerName = Request["customerName"]; 
var subjectLine = Request["subjectLine"]; 
var fileAttachment = Request["fileAttachment"]; 
try { 
// 初始化 
WebMail.SmtpServer = "smtp.126.com"; 
WebMail.SmtpPort = 25; 
WebMail.EnableSsl = false; 
WebMail.UserName = "zhuqi0"; 
WebMail.From = "[email protected]"; 
WebMail.Password = "**********"; 
// 创建包含附件的数组 
var filesList = new string [] { fileAttachment }; 
 // 添加附件和发送邮件 
WebMail.Send(to: "[email protected]",subject: subjectLine, 
body: "File attached. 
<br />From: " + customerName, 
filesToAttach: filesList); 

catch (Exception ex) 

<text> 
<b>邮件发送<em>失败</em></b> 
代码中没有提供正确的SMTP服务名,用户名,密码等信息。 
</text> 
}

<!DOCTYPE html>
<html>
<head> 
<title>求助中心</title>
</head>
<body> 
<p><b>@customerName</b>, 感谢你的支持.</p><p>关于下面问题的邮件已经发送给我们的客服,相关部门会及时处理。 <b>
@fileAttachment
</b>
file attached.
</p>
</body>
</html> 
ASP.NET MVC 3 Beta初体验之实用的WebMail

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

第一步:初始化,指定邮件发送服务器。

WebMail.SmtpServer = "smtp.126.com";    

第二步:指定端口。

WebMail.EnableSsl = false;  

第三步:指定用户名。

WebMail.UserName = "zhuqi0";     

第四步:你的邮箱地址和密码。

WebMail.From = "[email protected]";        
WebMail.Password = "********";   

第五步:如果有附件指定附件地址。

var filesList = new string [] { fileAttachment };

第六步:邮件发送。

WebMail.Send(to: "[email protected]",subject: subjectLine,            
body: "File attached. <br />From: " + customerName, 
filesToAttach: filesList); 

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

代码:http://files.cnblogs.com/zhuqil/MvcApplicationWebMail.rar




本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2010/10/23/1858818.html,如需转载请自行联系原作者