我有一個控制器發送一封電子郵件,並向我的ajax腳本返回「success」json響應。從控制器發送電子郵件時發生延遲
控制器看起來是這樣的:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EmailSend(string input1)
{
EmailSignup person = new EmailSignup { emailhasbeensent = false, Email = input1 };
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("Email", "PW");
MailMessage message = new MailMessage();
message.From = new MailAddress("Email");
message.To.Add(person.Email);
message.Subject = "Thank you for subscribing";
message.Body = "You have now subscribed for our newsletter";
message.IsBodyHtml = true;
client.Send(message);
person.emailhasbeensent = true;
return Json(new { status = "success" });
}
catch (Exception ex)
{
return Json(new { status = "error" });
}
}
不幸的是,成功的響應之前延遲發送,這意味着其電子郵件正在進入排序的頁面「空轉」它接收到的前成功的迴應。
有沒有辦法加快這個過程,以便立即發送響應? 如果沒有,那麼是否可以實施一個加載條,向用戶顯示「幕後」發生了什麼?
感謝您的回答!基本上,如果我將Sendemail代碼移動到另一個方法,那麼我的代碼會不會失敗,一旦生活就會失敗?據我瞭解,我通過這樣做防止了瓶頸? –