我有一种发送电子邮件的方法,它使用SmtpClient. 在此方法中,我有以下内容await smtpClient.SendMailAsync (mail);。调用是从我的控制器发出的,具体取决于具体情况:
[HttpPost]
[AllowAnonymous]
[ValidateJsonAntiForgeryToken]
public async Task<ActionResult> RegisterParticipant(RegisterIndexBaseVm attendee)
{
if (attendee == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
if (ModelState.IsValid)
{
attendee.EventId = Int32.Parse(Session["id"].ToString());
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(BaseUrl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage Res = await client.PostAsync("Register/RegisterAttendee", new StringContent(JsonConvert.SerializeObject(Mapper.ToParticipant(attendee)), Encoding.UTF8, "application/json"));
if (Res.IsSuccessStatusCode)
{
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<EmailTemplateDTO>(EmpResponse);
if (result.NotError == false)return Json(new { success = false, message = Constants.CulqiResponses[result.Response] }, JsonRequestBehavior.AllowGet);
if (string.IsNullOrEmpty(result.InvoiceInstitution))
{
if (result.SendEmail == true) MailHelper.SendMail(Enumerables.MailAction.EventRegisterSuccess, result);
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
在 Web 应用程序中,我有一个 AJAX 来执行对使用参数的所述控制器的调用async: true,但我总是收到 500 错误。我不太明白原因以及我应该如何正确调用每个方法和类以获得正确的结果。
答案如下:
An asynchronous module or handler completed while an asynchronous operation was still pending.
但是发送电子邮件没有任何不便。发送电子邮件的方法必须具有以下内容public static async void SendMail,我不希望控制器有任何真正的回应。
GCT1015
相关分类