你有沒有想過使用AJAX來實現它?
當用戶按下提交按鈕而不是回發到服務器時,創建2個ajax調用: 第一個是將文章保存到存儲庫(數據庫?)。
從服務器(可以包含文章ID)接收succesfull回答後,第二次調用Ajax調用發送郵件。服務器可以啓動一個線程發送郵件,這樣客戶端的答案將立即生效。
我調用Ajax調用的首選方法是使用JQuery:
$.ajax({
type: "POST",
url: "services.aspx/SubmitArticle",
data: "{articlecontent: '[put here the content you want to send]'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function(response) {
// Handle Error here. The response object contains the error details
},
success: function(response) {
// Check here if the article has been saved:
// response.d property contains the server answer. It can be boolean,
// integer, string or any other complex object
// If article saved, invoke here the send mail ajax call. assuming the response.d contains
// the article id:
sendMails(response.d);
// sendMails invokes another ajax similiar to this code snippest
}
});
在服務器端異步郵件發送方法可以看起來像:
[WebMethod]
public static bool SendMails(int articleId)
{
// Add the actual method that send mail to the thread pool
ThreadPool.QueueUserWorkItem(new WaitCallback(DoSendMail), articleId);
return true;
}
private void DoSendMail(object a)
{
int articleId = (int)a;
// Your code that sends mails goes here
}
要注意的是「後臺線程」在IIS/ASP.NET應用程序可能會導致IIS線程池出現問題... – Yahia 2012-03-14 09:49:59
hmmmmm ....你是什麼意思由後臺線程? – user1150440 2012-03-14 09:50:38
@Yahia你有什麼建議? – user1150440 2012-03-14 09:51:11