1

The Send-MailMessage command is great for what it is, but if I wanted to send hundreds (or thousands) of messages over SMTP, that would require hundreds\thousands of separate SMTP sessions.

Is there a way in PowerShell to submit multiple messages over SMTP with a single SMTP connection?

For example, I have 100 emails to send. Each email is unique (different set of recipients,sender, subject, and body). This isn't a case where I want to send a single email to multiple recipients. I'm looking to send SMTP mail over a single connection to the SMTP server and not open 100 separate SMTP connections to send my 100 e-mails.

Steven Presley
  • 116
  • 1
  • 8

2 Answers2

1

So I did a little testing by doing some network traces and interestingly enough when Send-MailMessage is used it opens a connection to an SMTP server and never closes it (so long as that PowerShell session I'm running is open). So this more or less solves my problem. I had always assumed that when Send-MailMessage is run it actually closes the connection when it is done, but it doesn't until the SMTP server forcibly closes it or the PowerSHhell process is closed.

The more you know.

Steven Presley
  • 116
  • 1
  • 8
0

The documentation says that "To" property is a collection. So you should be able to specify more than one email address at once without a need of one connection per recipient.

randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
  • well that is still a single email. I'm talking about submitting multiple non-unique emails. For example, I have 100 emails to send with different sets of recipients and different sets of bodies\subjects. SMTP absolutely supports this so I'm looking for a way to do it with PowerShell so I don't have to send the e-mails over 100+ separate SMTP connections. – Steven Presley Sep 10 '12 at 18:22
  • A look at one of the comment at earlier thread [Loop over SmtpClient.Send()](http://stackoverflow.com/questions/9083991/loop-over-smtpclient-send) says that "System.Net.Mail.SmtpClient doesn't keep a persistent connection for its lifetime...". From this thread it appears what your are trying to do is not possible. I presume is code snippet in the mentioned thread is what you are intending to code. – randominstanceOfLivingThing Sep 10 '12 at 19:02
  • It's kind of what I figured. I actually have a way to do it by writing over a raw TCP connection (System.Net.Sockets.TcpClient, System.Net.Sockets.NetworkStream and System.IO.StreamReader), but that is really...really ugly. – Steven Presley Sep 10 '12 at 19:09