Friday, May 8, 2009

You've Got Mail? Probably Junk Mail

The way I learn a new programming language is pretty much the same as anyone who knows other languages already:  I search for how to do the same things I've already done but using the new language.  That way I have a direct mapping of that-to-this.  It's basic human nature.  Comparison is a great way to identify uniqueness and establish familiarity with distinctions.  Holy cow!  That sounded like I actually knew what I was talking about!  Whatever I had for lunch, I will be sure to have it more often.

So, sending an e-mail message from within a script is probably one of the most boring and mundane tasks for someone writing script code.  Maybe not as boring and mundane as processing folders and files or reading and writing Excel spreadsheets or Access databases.  Ok, maybe not as boring and mundane as reading my drivel.  Whatever.

In any case, for comparison sake, here's (as you probably already expected) a chunk of code to send an SMTP mail message in three different languages:  VBScript, KiXtart and PowerShell (version 2.0 CTP in this case).  In the first two, I'm relying on the CDOSys COM interface library (API).  For the last (PowerShell) I use the .NET flavor, not because it's nifty and cooler.  It's because PowerShell v2 doesn't support CDOSys due to problems with "reflection" and source libraries (or lack thereof).

VBScript Example


Sub SendMail(from, sendTo, subject, message)
Dim objMessage
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = subject
objMessage.From = from
objMessage.To = sendTo
objMessage.TextBody = message
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mysmtpserver.mycompany.local"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
Set objMessage = Nothing
End Sub


KiXtart Example



Function SendMail($from, $sendTo, $subject, $message)
Dim $objMessage, $server, $s1, $s2, $s3
$s1 = "http://schemas.microsoft.com/cdo/configuration/sendusing"
$s2 = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
$s3 = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
$server = "mysmtpserver.mycompany.local"
$objMessage = CreateObject("CDO.Message")
$objMessage.Subject = $subject
$objMessage.From = $from
$objMessage.To = $sendTo
$objMessage.TextBody = $message
$objMessage.Configuration.Fields.Item($s1) = 2
$objMessage.Configuration.Fields.Item($s2) = $server
$objMessage.Configuration.Fields.Item($s3) = 25
$objMessage.Configuration.Fields.Update
$objMessage.Send
$objMessage = 0
EndFunction


PowerShell v2 (CTP) Example



Function SendMail ([string]$from, [string]$sendto, [string]$subject, [string]$msg) {
$SmtpClient = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$SmtpClient.Host = "mysmtpserver.mycompany.local"
$mailmessage.from = ($from)
$mailmessage.To.add($sendto)
$mailmessage.Subject = $subject
$mailmessage.Body = $msg
try {
$smtpclient.Send($mailmessage)
}
catch [System.Net.WebException],[System.Net.Mail.SmtpException] {
"an error occurred: unable to resolve smtp host name"
}
catch {
"an error occurred: unable to send message (details follow)..."
$error[0].Exception.ToString()
}
}

SendMail "you@huh.local" "mom@what.local" "Hey Mom!" "Happy Mother's Day!"

Some notes about the code mess shown above:

These examples are not uniquely "my own" creations. They are derived from publicly-available examples from each of the prospective vendors (Microsoft, KiXtart) and many, many, many web sites and discussion forums. The examples do not show every available feature, such as CC or BCC options, formatting, and so on. They are simply meant to provide a quick visual comparison to how they approach the same task (sending an e-mail message). In that sense, I hope they help. Drop a comment if you want to share your thoughts or ask questions.

2 comments:

JTB World said...

Have you played the prank to send email within your network that comes from Bill.Gates@microsoft.com or the like?

skatterbrainz said...

The environments I work in don't have a sense of humor like that. Which is part of the reason I let my semi-quasi-humor side get out of control when doing things outside of work. Although, at one former job we had an admin that refused to follow rules about keeping people out of a sensitive admins group on the network. So I wrote a script to fire a script whenever an event was triggered by him involving the group that locked his user account. It was kind of fun. Ahhh, the good times.