Developer

Using a hybrid XSLT solution to send an email

Edmond Woychowsky recounts how a hybrid XSLT solution that he used recently reminds him of his college days.

When I was in college, it was common to learn more than one programming language at a time, and it was just as common to accidentally switch languages mid-program. Though this language switch never worked, students always had one of two valid reasons for it. One reason was that we knew how accomplish to parts of the task in one language but not the other. The other reason (which occurred more towards the end of the semester than at the beginning) was that the language just didn't support what we were trying to accomplish. So, in any given semester, you'd see a program that looked like it was out of John Carpenter's The Thing, frozen mid-morph between two very different programming languages.

Things have changed considerably since my college days. When I switch languages mid-program I mean to make the change, and the best part is now it works. Although I have to admit that the reason for the switch still falls into one of the two reasons, though usually the second reason. I'll share a recent example of when I had to use a hybrid XSLT solution.

I needed to send an email; this would normally be a simple enough task, but the program also needed to do some heavy duty transformations, so it was written in MSXML XSLT, version 1. A lot of things can't be accomplished in straight XSLT, and this is one of them. Fortunately, there seems to be a rule that Microsoft can't blindly follow W3C recommendations without tweaking it. So, Microsoft added a few extensions, including one that allows a developer to add extension functions in JavaScript, VBScript, VB.NET, and C#. Since I know how to send an e-mail in C#, the choice was made for me. Listing A shows the C# function to send an e-mail.

Listing A

C# function to send an e-mail

/*

Send an email.

*/

public bool mail(string server, string format, string sender, string recipient, string subject, string body)

{

try

{

System.Net.Mail.SmtpClient scMail = new System.Net.Mail.SmtpClient(server);

System.Net.Mail.MailMessage mmMail = new System.Net.Mail.MailMessage(sender, recipient, subject, body);

mmMail.BodyEncoding = Encoding.UTF8;

mmMail.IsBodyHtml = (format == "htm") ? true : false;

scMail.Send(mmMail);

return true;

}

catch (Exception e)

{

return false;

}

}

My biggest issue was finding the address of my SMTP server; once I found the address, all I had to do was wrap the C# in the appropriate XSLT, as shown in Listing B.

Listing B

XSLT that sends an e-mail

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cs="urn:cs">

<msxsl:script language="CSharp" implements-prefix="cs"><![CDATA[

/*

Send an email.

*/

public bool mail(string server, string format, string sender, string recipient, string subject, string body)

{

try

{

System.Net.Mail.SmtpClient scMail = new System.Net.Mail.SmtpClient(server);

System.Net.Mail.MailMessage mmMail = new System.Net.Mail.MailMessage(sender, recipient, subject, body);

mmMail.BodyEncoding = Encoding.UTF8;

mmMail.IsBodyHtml = (format == "htm") ? true : false;

scMail.Send(mmMail);

return true;

}

catch (Exception e)

{

return false;

}

}

]]></msxsl:script>

<xsl:template match="/">

<xsl:variable name="smtpServer">something.whatever.com</xsl:variable>

<xsl:variable name="format">text</xsl:variable>

<xsl:variable name="sender">sender@whatever.com</xsl:variable>

<xsl:variable name="recipient">recipient@whatever.com</xsl:variable>

<xsl:variable name="subject">Test</xsl:variable>

<xsl:variable name="body">Test message.</xsl:variable>

<xsl:element name="email">

<xsl:value-of select="cs:mail($smtpServer,$format,$sender,$recipient,$subject,$body)"/>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

I'm constantly amazed by how many of the things I learned in college still apply in development — even the screw-ups. Yes, it still looks like code frozen in mid-morph between one thing and another, but unlike college or (The Thing), I'm not going to lose any sleep over this solution.

Get weekly development tips in your inbox Keep your developer skills sharp by signing up for TechRepublic's free Web Developer newsletter, delivered each Tuesday. Automatically subscribe today!

Editor's Picks

Free Newsletters, In your Inbox