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

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

不带附件的邮件发送

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

复制代码
代码
[HttpGet]
public  ActionResult EmailRequest() 

return  View(); 
}

[HttpPost]
public  ActionResult ProcessRequest()
{
return  View();
}
复制代码

EmailRequest.cshtml代码如下:

复制代码
代码
<! 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 >
复制代码

 发送邮件的View:

复制代码
@{ 
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 = "zhuqi0@126.com"; 
WebMail.Password = "**********"; 
// 发送邮件 
WebMail.Send(to:"zhuqi0@126.com", 
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 >  
复制代码

运行:

发送成功页面

邮件通知:

带附件的邮件发送:

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

复制代码
@{ 
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 = "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); 

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 >  
复制代码

从上面的两种情况我们可以看到,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://files.cnblogs.com/zhuqil/MvcApplicationWebMail.rar




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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值