c# - ASP.NET Web API Async Tasks, sending Mails -
my application has massive performance problems. found problem comes sending of emails. how can fix problem method registeruser
returns, while sending of email still in process? tried starting thread run sendemailconfirm
method, gave me objectdisposedexception
in sendemailconfirm
public async task<identityresult> registeruser(accountviewmodels.registerviewmodel usermodel) { var result = await _usermanager.createasync(user, usermodel.password); this.sendemailconfirm(usermodel.email); return result; } public async void sendemailconfirm(string mail) { string subject = "please confirm email chronicus"; string body = "hello" string email = user.email; _messageservice.sendmail(mail, subject, body); } public void sendmail(string receiver, string subject, string body) { this._msg = new mailmessage(username, receiver); this._msg.from = new mailaddress(username, name); this._msg.subject = subject; this._msg.body = body; this._msg.isbodyhtml = true; this._smtpclient.send(_msg); }
edit: added sendmail method question
you need use sendmailasync
method of smtpclient
class.
also, should return task
async method return no value.
here how code like:
public async task<identityresult> registeruser(accountviewmodels.registerviewmodel usermodel) { var result = await _usermanager.createasync(user, usermodel.password); await this.sendemailconfirm(usermodel.email); return result; } public task sendemailconfirm(string mail) { string subject = "please confirm email chronicus"; string body = "hello" string email = user.email; return _messageservice.sendmail(mail, subject, body); }
and here how sendmail
like:
public task sendmail(string receiver, string subject, string body) { this._msg = new mailmessage(username, receiver); this._msg.from = new mailaddress(username, name); this._msg.subject = subject; this._msg.body = body; this._msg.isbodyhtml = true; return this._smtpclient.sendmailasync(_msg); }
Comments
Post a Comment