ASP.NET MVC 3 Beta初體驗之實用的WebMail

來源:互聯網
上載者:User

    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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.