2

I'm trying to find a .Net equivalent of the java code in this question: How to send multiple emails in one session? Amazingly, this question hasn't been answered yet on Stack Overflow (or I'm not searching with the right terms.)

I want to be able to connect to my SMTP server (this is a 3rd party server, not under my control) and send up to 500 emails or so at one time. These emails are requested by our users, and are all unique. I know I could loop through a list and send them sequentially, or even use threading to spawn multiple processes, but this seems to be wasteful. I'd be opening a connection, sending one email, then closing the connection.

I've seen it implied that .Net will cache an SMTP connection, kind of like database connection pooling, but I can't find confirmation. Plus I'd be relying on code that may have unintended side effects for the SMTP that I'm using.

Has anyone done this in .Net? Did you use a 3rd party component? Did you instead just manually implement the SMTP RFC? I really don't want to have to do that.

Community
  • 1
  • 1
Matt Dawdy
  • 19,247
  • 18
  • 66
  • 91

1 Answers1

1

You can create a SMTP object with

SMTPClient

so create on of those and then call the Send method for each email you need to send.

You can't really send all those mails at 'once' but you can send them without recreating the SMTP object.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • 2
    Yep, straight from the docs, it will pool until you `Dispose`: "The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server" – Chris Haas May 08 '12 at 03:13
  • Reading the docs? Novel idea! I can't believe I missed that. Thanks for the answer and the comment, both of you. – Matt Dawdy May 08 '12 at 03:20