Powershell SMTP mail sending script

by smthng 30. October 2007 12:46

Let's get back into the techie side of things for just a bit...

At the office, we've been plagued by a really weird Exchange server issue and I've needed to do some "testing" after multiple attempts to fix it.  I wanted a quick and easy method for sending out test messages on a regular basis and I managed to whip up a really simple powershell script to get the job done...  here it be:

$i = 0
$msgNum = read-host -prompt "Enter number of messages to send"
[int]$msgTime = read-host -prompt "How long to wait between messages (minutes)"
$msgSecs = $msgTime * 60
$sc= new-object Net.Mail.SmtpClient -arg "YourServerName"
for ( $i = 1 ; $i -le $msgNum; $i++ ) {
    $time = Get-Date -f "HH:mm:ss"
    write-host "About to send a message at" $time
    $sc= new-object Net.Mail.SmtpClient -arg "YourServerName"
    $sc.Send("me@myaddress.com","destination@siteaddress.com",$time,"Here be a message.")
    Write-host "I've sent" $i "messages so far.  Now sleeping for" $msgTime "minutes."
    Write-host "           ----------------"
    start-sleep -s $msgSecs
}

Here's the quick and dirty on this...  You need to have the SMTP service installed and working on the server you specify.  Change both of the YourServerName entries to match the server that has the SMTP service.  Change the me@myaddress.com to an address that you want to send the message from (it needs permission to send in Exchange and SMTP), change the destination@siteaddress.com to the address you want to send to (in our case, it was a large distribution list).  Run the script.  It'll make a message and send it off.  The subject will be the time (in the format of hh:mm:ss) and the body will be "Here be a message" (you can change that if you like).  It will loop the number of times you specify and it will pause for the number of minutes you enter between each message. 

It's not elegant, nor efficient, but it get's the job done quickly and easily. If you enter bogus numbers to freak out the script, it will happily let you and your results will be unpredictable.  But, if you enter numbers that make sense, it'll work just fine.

It took me a while to track down the whole Net.Mail.SmtpClient stuff, so I figured I'd post it up here in case anyone else needs it.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

blog

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About smthng

Just a guy who digs Jeeps, photography, podcasts, sci-fi, running, motorcycles, and stuff.

Tags