c# - Are the SmtpClient.SendMailAsync methods Thread Safe? -


the smtpclient class states instance members not thread safe. can seen if concurrent calls made send or sendasync. both methods throw invalidoperationexception on second call if first has not yet completed.

the method sendmailasync, introduced in .net 4.5, not list invalidoperationexception thrown exception. new .net 4.5 methods implement sort of queuing? reflector isn't able shed light on implementation details of class, assume has been implemented in native methods.

can multiple threads call sendmessageasync method on shared instance of smtp client safely?

i'm not sure why using reflector didn't work you. if decompile it, see following code:

[hostprotection(securityaction.linkdemand, externalthreading=true)] public task sendmailasync(mailmessage message) {     taskcompletionsource<object> tcs = new taskcompletionsource<object>();     sendcompletedeventhandler handler = null;     handler = delegate (object sender, asynccompletedeventargs e) {         this.handlecompletion(tcs, e, handler);     };     this.sendcompleted += handler;     try     {         this.sendasync(message, tcs);     }     catch     {         this.sendcompleted -= handler;         throw;     }     return tcs.task; } 

as can see, it's simple tap wrapper sendasync(). , if sendasync() throws exception, sendmailasync() rethrows it.

so, conclusion sendmailasync() not thread-safe , exceptions underdocumented.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -