IntroductionIn this article, I will explain send email using XLST template in .net. The most time we have to send email to users in any kind applications. When we are send email to user, that email must be well formatted and look feel also must be good. About XLSTThis specification defines the syntax and semantics of XSLT, which is a language for transforming XML documents into other XML documents.XSLT is designed for use as part of XSL, which is a stylesheet language for XML. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary. XSLT is also designed to be used independently of XSL. However, XSLT is not intended as a completely general-purpose XML transformation language. Rather it is designed primarily for the kinds of transformations that are needed when XSLT is used as part of XSL.
More about xlst here: http://www.w3.org
ImplementationUnder the implementation, I have selected a sample a format to send email error notification to users, then created a XLST template using visual studio or any other tools.Create a simple windows applicaiton using visual studio. The error notification xlst template like following, <?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><table border="1" align="center" cellpadding="0" cellspacing="0"><tr align="center" bordercolor="#983222"><td align="center"><table style="FONT-WEIGHT: bold; FONT-SIZE: 12px; BACKGROUND: #efefef; MARGIN: 0px auto" width="700" border="0" cellpadding="0" cellspacing="0"><thead><tr><td vAlign="middle" align="center" bgcolor="#0066cc" height="20"><font style="FONT-SIZE: 14px; COLOR: white; FONT-FAMILY: Verdana">For Your Action</font></td></tr><tr><td style="FONT-SIZE: 10px; COLOR: black; FONT-FAMILY: Verdana"><br/>MESSAGE: This is a system generated email notification. Please do not reply to this email.<br/><br/></td></tr><tr><td vAlign="middle" align="center" bgcolor="#0066cc" height="20"><font style="FONT-SIZE: 13px; COLOR: white; FONT-FAMILY: Verdana">Message</font></td></tr><tr><td style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana"><br/><p>Dear All,<br/><br/><table style="FONT-WEIGHT: bold; FONT-SIZE: 10px; BACKGROUND: #efefef; MARGIN: 0px auto" width="800" border="0" cellpadding="0" cellspacing="0"><tr><td width="50"></td><td width="750"><p><div style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana">Please find the below issue:</div><br/></p></td></tr></table></p></td></tr><tr><td style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana"><p><table style="FONT-SIZE: 10px; BACKGROUND: #efefef; MARGIN: 0px auto" width="800" border="0" cellpadding="0" cellspacing="0"><tr><td width="70"></td><td width="730"><p><div style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana"><ul><li><font style="FONT-WEIGHT: 400; FONT-SIZE: 11px">{ErrorMsg}</font></li></ul></div></p></td></tr></table></p></td></tr><tr><td style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana"><p><table style="FONT-WEIGHT: bold; FONT-SIZE: 10px; BACKGROUND: #efefef; MARGIN: 0px auto" width="800" border="0" cellpadding="0" cellspacing="0"><tr><td width="50"></td><td width="750"><p><div style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana">Thank you.</div><br/></p></td></tr></table></p></td></tr><tr><td style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana"><table width="800" cellSpacing="0" cellPadding="0" border="0"><tr><td width="100%" align="left" style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana; FONT-WEIGHT: bold;"><br/>Best Regards<br/><br/></td></tr><tr><td width="100%" align="left" style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana; FONT-WEIGHT: bold;"><em>TTAS Team</em><br/><br/><br/></td></tr></table></td></tr><tr><td vAlign="middle" align="left" bgColor="#0066cc" height="20"></td></tr></thead></table></td></tr></table></body></html></xsl:template></xsl:stylesheet>Now We have write code to process this xlst template with our real data usign vb.net.to process xlst using xml, you have to know about xmlt little bit, then you able to under this code. Private Sub SendErrorEmail(ByVal PErrorMessage As String)Dim vIsEmailSend As StringDim PEmailFrom As StringDim PEmailTo As StringDim PEmailCC As StringDim PEmailTitle As StringDim vMainPath As StringDim vErrorEmailTemplate As StringDim vsmtpServer As StringDim vsmtpPort As IntegerDim vusername As StringDim vpassword As StringTryvMainPath = System.Windows.Forms.Application.StartupPath
vIsEmailSend = ConfigurationManager.AppSettings("EmailOn")If UCase(vIsEmailSend.Trim) = "YES" ThenvErrorEmailTemplate = "EmailNotify.xslt"Dim objxslt As New XslTransform()If File.Exists(Path.Combine(vMainPath, vErrorEmailTemplate)) = False ThenExit SubEnd IfPEmailFrom = ConfigurationManager.AppSettings("FromEmail")PEmailTo = ConfigurationManager.AppSettings("ToEmail")PEmailCC = ConfigurationManager.AppSettings("CCEmail")PEmailTitle = "The error Notification:<date>"vsmtpServer = ConfigurationManager.AppSettings("SMTPServer")vsmtpPort = ConfigurationManager.AppSettings("SMTPPort")vusername = ConfigurationManager.AppSettings("UserName")vpassword = ConfigurationManager.AppSettings("Password")objxslt.Load(Path.Combine(vMainPath, vErrorEmailTemplate))Dim xmldoc As New XmlDocument
xmldoc.AppendChild(xmldoc.CreateElement("DocumentRoot"))Dim xpathnav As XPathNavigator = xmldoc.CreateNavigator()Dim xslarg As New XsltArgumentListDim emailbuilder As New StringBuilderDim xmlwriter As New XmlTextWriter(New System.IO.StringWriter(emailbuilder))objxslt.Transform(xpathnav, xslarg, xmlwriter, Nothing)Dim bodytext As StringDim xemaildoc As New XmlDocument
xemaildoc.LoadXml(emailbuilder.ToString())Dim bodynode As XmlNode
bodynode = xemaildoc.SelectSingleNode("//body")bodytext = bodynode.InnerXmlIf bodytext.Length > 0 Thenbodytext = bodytext.Replace("{ErrorMsg}", PErrorMessage)End IfDim vEmailTitle As String = PEmailTitle.Replace("<date>", Now.ToString("yyyyMMdd HH:MM:ss"))Dim vMailMessage As New MailMessage
vMailMessage.From = New MailAddress(PEmailFrom, "www.codegain.com")vMailMessage.To.Add(PEmailTo)vMailMessage.CC.Add(PEmailCC)vMailMessage.Subject = vEmailTitle
vMailMessage.Body = bodytext
vMailMessage.BodyEncoding = Encoding.UTF8
vMailMessage.IsBodyHtml = TrueDim smtpClient As New SmtpClient(vsmtpServer, vsmtpPort)If String.IsNullOrEmpty(vusername) And String.IsNullOrEmpty(vpassword) ThensmtpClient.UseDefaultCredentials = TrueElsesmtpClient.Credentials = New Net.NetworkCredential(vusername, vpassword)End If smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.Send(vMailMessage)End IfCatch ex As SmtpExceptionCatch e As ExceptionEnd TryEnd SubIn above code, create XML document object with xlst template, then parse the xlst to using XPathNavigator and find the documentRoot.Once find the root element, and then you need find the tags to replace with original value from the system. If bodytext.Length > 0 Thenbodytext = bodytext.Replace("{ErrorMsg}", PErrorMessage)End IfDim vEmailTitle As String = PEmailTitle.Replace("<date>", Now.ToString("yyyyMMdd HH:MM:ss"))Just call above function inside the button click. Once we are replace values, then send the email with body text, final email result will be following. Sample Email OutputConclusionIn this article, I have explian send email with a custom template usign XLST in VB.NET.i hope this is help to you all.and you able to modify this template easilly and use in your application to send email with custom format. Sample Project SourceDownload source files -51 kb |