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 _
("") = 2
objMessage.Configuration.Fields.Item _
("") = "mysmtpserver.mycompany.local"
objMessage.Configuration.Fields.Item _
("") = 25
Set objMessage = Nothing
End Sub

KiXtart Example

Function SendMail($from, $sendTo, $subject, $message)
Dim $objMessage, $server, $s1, $s2, $s3
$s1 = ""
$s2 = ""
$s3 = ""
$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 = 0

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.Subject = $subject
$mailmessage.Body = $msg
try {
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)..."

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.
Post a Comment