In my MVC application, I am having a functionality of sending message to the respective owners/dealers, by clients. The message model is as below:
public class Messages
{
public string MessageFrom { get; set; }
public string MessageUserEmailID { get; set; }
public string MessageUserContactNum { get; set; }
public string Message { get; set; }
public int UserID { get; set; }//Admin user id
public string propertyName { get; set; }
}
and below is my Controller method which will be called through AJAX where
- I store the message in DB
- Send the admin a notification through email
- return a
Jsonresponse back to user.
HomeController
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendMessage([Bind(Prefix = "messageModel")]Messages model)
{
private SendNotification notify = new SendNotification();
if (ModelState.IsValid)
{
using (db)
{
tblMessage messageModel = new tblMessage();
//....
//....
//....
//fill the messageModel with the model values
db.tblMessages.Add(messageModel);
db.SaveChanges();
string toAddress = db.tblUsers.Where(u => u.UserId == model.UserID).Select(x => x.UserEmailID).FirstOrDefault();
if (!string.IsNullOrEmpty(toAddress))
{
notify.NotifyUser(toAddress, model.MessageUserContactNum, model.propertyName, model.Message, model.MessageFrom, model.MessageUserEmailID, "Kemmale Engineers' Notification - You have a message");
}
return Json(new { result = true, message = "Message sent! Thank you! We will get back to you soon!" }, JsonRequestBehavior.AllowGet);
}
}
return Json(new { result = false, message = "Server issue! Please try again later" }, JsonRequestBehavior.AllowGet);
}
SendNotification class contains NotifyUser method which is as below:
public void NotifyUser(string toAddress, string userPhoneNum, string propertyName, string userMessage, string userName, string userContactEmail, string subject)
{
using (MailMessage mail = new MailMessage())
{
string Password = WebConfigurationManager.AppSettings["ApplicationEmailPassword"];
var securePassword = new SecureString();
Array.ForEach(Password.ToArray(), securePassword.AppendChar);
securePassword.MakeReadOnly();
mail.To.Add(toAddress);
mail.From = new MailAddress(WebConfigurationManager.AppSettings["ApplicationEmailID"]);
mail.Subject = subject;
mail.Body = Convert.ToString(ConstructBody(userMessage, propertyName, userPhoneNum, userContactEmail, userName));
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(Convert.ToString(mail.From), securePassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
}
}
As you can see that the Notify has return type void. Now how can I return Json message to the user without waiting for Notify method to get executed, since it is very obsolete to make user wait for internal process to complete which is irrelevant to the user? I am thinking of Asynchronous Task but not sure how can I implement it here.