I have a button that - when the user clicks it - sends an email. I'd love for this to just return immediately and send the email in the background without holding up the UI as the email is processed. I've searched around for async
/await
/etc and I've found so many different approaches - I'm looking for a simple solution to this. My email code:
public void SendEmail(string toAddress, string subject, string body, string code = null) {
try {
var fromAddressObj = new MailAddress("noreply@me.com", "Name");
var toAddressObj = new MailAddress(toAddress, toAddress);
const string fromPassword = "Password";
var smtp = new SmtpClient {
Host = "smtp.office365.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddressObj.Address, fromPassword)
};
using (var message = new MailMessage(fromAddressObj, toAddressObj) {
Subject = subject,
IsBodyHtml = true
}) {
message.Body = body;
smtp.Send(message);
}
} catch (Exception e) {
Elmah.ErrorSignal.FromCurrentContext().Raise(e);
}
}
How can I modify this so that the caller is not blocked?