我试图通过发送电子邮件来避免等待时间 退出页面之前。
页面/应用程序在退出之前正在执行一系列操作,如下所示:
- 数据库交互
- 发送电子邮件(向经理通报该交易的最新情况)
- 退出页面/应用程序。
函数背后的代码:
ExecEntryOnTbl(SQL);// <-- update / insert to database
sendMailNote(action);// <-- send mail with notification of update
exitTC(action, custid);<-- exit page.
done via javascript :
window.location.href = "someOtherPage.aspx"
from code behind via
RegisterClientScriptBlock(...)
我想解决这个问题:
如何避免等待 sendMailNote()
完成
在执行以下 exitTC()
之前。这可能吗?
更新电子邮件类别/方法
public static class mail { public static string aReciver, bSubject, cBody; public static void sendMailNoteExc() { string SmtpServer = "smtp.gmail.com"; int port = 111; string sender = "aaa@bbb.com"; string ReCiver = aReciver; string Subject = bSubject; string Body = cBody; string account = "acc@domain.com"; string Pass = "a123456"; Send(SmtpServer, port, account, Pass, sender, ReCiver, Subject, Body); } public static void Send(string smtpServer,int Port,string Account, string PassWord, string From, string To, string Subject, string Body) { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(smtpServer); mail.From = new MailAddress(From); mail.To.Add(To); mail.Subject = Subject; mail.Body = Body; SmtpServer.Port = Port; SmtpServer.Credentials = new System.Net.NetworkCredential(Account, PassWord); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); } }
最佳答案
只要您使用 C# 4.0,您就可以利用 Task Parallel Library并在后台线程中运行 sendMailNote(action)
,如下所示:
Task.Factory.StartNew(() => sendMailNote(action));
只需确保添加代码来处理发送电子邮件期间发生的任何异常。然而,更好的解决方案是从用户界面代码中完全删除此逻辑,并在 Windows 服务或云 worker Angular 色等后台任务中执行它。
关于javascript - 发送电子邮件而不等待操作完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941168/