c# - How to Send Mail Async -
i running issue when trying send email async, found out no of post on stackoverflow none of them helpful. have following block of code
public class emailservice : iidentitymessageservice { public task sendasync(identitymessage message) { // plug in email service here send email. var mailmessage = new mailmessage ("me@example.com", message.destination, message.subject, message.body); mailmessage.isbodyhtml = true; var client = new smtpclient(); client.sendcompleted += (s, e) => client.dispose(); client.sendasync(mailmessage,null); return task.fromresult(0); } }
i got email getting exception when block of code run
an asynchronous module or handler completed while asynchronous operation still pending.
any suggestion?
use sendmailasync
instead of sendasync
:
public class emailservice : iidentitymessageservice { public async task sendasync(identitymessage message) { // plug in email service here send email. var mailmessage = new mailmessage ("me@example.com", message.destination, message.subject, message.body); mailmessage.isbodyhtml = true; using(var client = new smtpclient()) { await client.sendmailasync(mailmessage); } } }
Comments
Post a Comment