您的位置:知识库 » .NET技术

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

作者: 麒麟  来源: 博客园  发布时间: 2010-11-02 14:32  阅读: 938 次  推荐: 0   原文链接   [收藏]  

  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

0
0
 
标签:ASP.NET MVC 3

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻