Is Smtp.SendAsync thread-safe?

0 votes
asked Sep 16 by bjallemad (150 points)

Hi,

I'm just wondering if Smtp.SendAsync is thread-safe?
For example in a windows service, can I do Connect/Login in startup of the windows service (or when first used), and then resuse the same Smtp object until the service stops?

That is: multiple threads may call Smtp.SendAsync simultaniously on the same Smtp object instance!

If so, is it recommeded to reuse the Smtp object in such scenarios?

Regards
Andreas

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Sep 16 by Lukas Pokorny (115,410 points)
selected Sep 16 by bjallemad
 
Best answer

All Smtp methods are thread-safe in the sense that they fail if called while another method is in progress.

Reusing the same Smtp session is possible (and useful if you need to send more messages through one SMTP server), but you have to keep track of whether a method is currently active on a specific Smtp instance yourself.

However, please note that when connected to third-party SMTP servers, keeping connections idle unnecessarily (until the service stops or until they fail) would not be a good practice.

...