Send email using template in vb.net

No.of Views2183
Bookmarked1 times
Downloads 
Votes0
By  RRaveen   On  03 Jun 2010 10:06:41
Tag : VB.NET , Miscellaneous
Send email using XLST template in vb.net, and also code will help to send email in asp.net as well.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In 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 XLST

This 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

Implementation

Under 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 Sub

In 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 Output

Image Loading

Conclusion

In 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 Source

Download source files -51 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top