stmpclient sendasync() 发送邮件失败 现在无法进行异步操作,求助大神,如何修改?
先说下我的思路,在注册页面注册时,用户点击注册按钮后,将用户填写的字段(如用户名,密码,邮箱等)写入数据库, 并【异步发送邮件】.
异步发送邮件时 报错,邮件发送失败,现在无法开始异步操作!!
下面是代码和错误
http://asp.net mvc Razer 视图引擎下
注册时的控制器RegCtronller:
[HttpPost]
public ActionResult Index()
{
.............
//获取用户填写的字段
Utils.SendEmail(Email, UserName + ",恭喜您注册成为xx人才网的企业用户!", sendcontent); //email 为用户输入的邮箱 username 是用户输入的用户名
.......
//返回json
}
----------------------------------
utils类中 的 异步发送邮件方法
public static bool SendEmail(string recipient, string subject, string body)
{
.....字数限制 所以省略
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
...
smtp.SendAsync(myMail,userState); //【异步发送 ,此处报错】
return true;
}
捕捉到 System.Net.Mail.SmtpException
HResult=-2146233088
Message=发送邮件失败。
Source=System
StackTrace:
在 System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=现在无法开始异步操作。异步操作只能在异步处理程序或模块中开始,或在页生存期中的特定事件过程中开始。如果此异常在执行 Page 时发生,请确保 Page 标记为 <%@ Page Async="true" %>。
Source=System.Web
StackTrace:
在 System.Web.AspNetSynchronizationContext.OperationStarted()
在 System.ComponentModel.AsyncOperation.CreateOperation(Object userSuppliedState, SynchronizationContext syncContext)
在 System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
InnerException: