Monday, May 16, 2011

Step 1:Create an XML

<RedeemNonStayRequest>
    <Email></Email>
    <MembershipNo></MembershipNo>
    <ItemCode></ItemCode>
    <Description></Description>
    <Comments></Comments>
    <MailingAddress></MailingAddress>
    <Landmark></Landmark>
</RedeemNonStayRequest>

Step 2:Create XLST

<?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" exclude-result-prefixes="msxsl">
    <xsl:param name="pEmail"></xsl:param>
    <xsl:param name="pMembershipNo"></xsl:param>
    <xsl:param name="pItemCode"></xsl:param>
    <xsl:param name="pDescription"></xsl:param>
    <xsl:param name="pComments"></xsl:param>
    <xsl:param name="pMailingAddress"></xsl:param>
    <xsl:param name="pLandmark"></xsl:param>
    <xsl:template match="/">
        <Table border="1" cellspacing="0" cellpadding="1">
          <tr>
            <td align="center" style="FONT-WEIGHT: bold;FONT-SIZE: 10px;COLOR: #666666;LINE-HEIGHT: 15px; FONT-FAMILY: Verdana;TEXT-DECORATION: none">Taj Advantage Plus Redeem Request</td>
          </tr>
            <tr>
                <td align="center">
                    <table width="500px" style="FONT-WEIGHT: bold;FONT-SIZE: 10px;COLOR: #666666;LINE-HEIGHT: 15px; FONT-FAMILY: Verdana;TEXT-DECORATION: none">
                     
                        <xsl:apply-templates select ="//Email"></xsl:apply-templates>
                        <xsl:apply-templates select="//MembershipNo"></xsl:apply-templates>
                        <xsl:apply-templates select="//ItemCode"></xsl:apply-templates>
                        <xsl:apply-templates select="//Description"></xsl:apply-templates>
                        <xsl:apply-templates select="//Comments"></xsl:apply-templates>
                        <xsl:apply-templates select="//MailingAddress"></xsl:apply-templates>
                        <xsl:apply-templates select="//Landmark"></xsl:apply-templates>                       
                    </table>
                </td>
            </tr>
        </Table>
    </xsl:template>
    <xsl:template match='Email'>
        <tr>
            <td style='text-align:right'>
                Email
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pEmail'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='MembershipNo'>
        <tr>
            <td style='text-align:right'>
                Membership No
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pMembershipNo'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='ItemCode'>
        <tr>
            <td style='text-align:right'>
                Item Code
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pItemCode'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='Description'>
        <tr>
            <td style='text-align:right'>
                Description
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pDescription'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='Comments'>
        <tr>
            <td style='text-align:right'>
                Comments
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pComments'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='MailingAddress'>
        <tr>
            <td style='text-align:right'>
                Mailing Address
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pMailingAddress'/>
            </td>
        </tr>
    </xsl:template>
    <xsl:template match='Landmark'>
        <tr>
            <td style='text-align:right'>
                Landmark
            </td>
            <td style='text-align:left'>
                <xsl:value-of select='$pLandmark'/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Step 3:

Add these namespaces

using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.Web.Mail;

Step 4:

Add the following code to send mail using XLST template


private void SendMail()
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("EmailFormat.xml"));
        XslCompiledTransform xct = new XslCompiledTransform(true);
        xct.Load(Server.MapPath("EmailFormat.xslt"));

        //add arguments for placeholders in xlst(ie. substitute the values in XLST before send the email)
        XsltArgumentList xArgs = new XsltArgumentList();
        xArgs.AddParam("pEmail", "", txtEmailID.Text);
        xArgs.AddParam("pMembershipNo", "", txtMemID.Text);
        xArgs.AddParam("pItemCode", "", lblCode.Text);
        xArgs.AddParam("pDescription", "", lbldesc.Text);
        xArgs.AddParam("pComments", "", txtComments.Text);
        xArgs.AddParam("pMailingAddress", "", txtMailingAddress.Text);
        xArgs.AddParam("pLandmark", "", txtLandMark.Text);

        //process the xlst in memory using StringWriter
        StringWriter mailbody = new StringWriter();
       
        //transform it
        xct.Transform(xDoc, xArgs, mailbody);

        //send mail now
        MailMessage mm = new MailMessage("From addess","To address");
        mm.Subject = "Mail Subject";

        //transformed output from stringwriter stream
        mm.Body = mailbody.ToString();

        //HTML is very important
        mm.IsBodyHtml = true;
        SmtpClient cli = new SmtpClient("SMTP servere IP address");
        cli.Send(mm);
        objMail.Body = mailbody.ToString();
        SmtpMail.Send(objMail);
        Response.Redirect("Thankyou.aspx");
    }

No comments:

Post a Comment