现在的位置: 首页 > 综合 > 正文

使用XSLT制作HTML邮件模板并发送

2013年10月19日 ⁄ 综合 ⁄ 共 10240字 ⁄ 字号 评论关闭

使用XSLT制作HTML邮件模板并发送

之前有写过能发送邮件的小工具,基本做法就是先在代码中写死一封HTML邮件的格式,然后用适当的方法取得外部的几个值填到预设的参数里,最后把合体后的string类型邮件发出去。简单的邮件还好,当内容稍微多一点,比如输出数据库的一个表,又或者邮件格式经常要变动的,就感觉很麻烦很受拘束。

后来看到了别人用XSLT做的一个邮件模板,很受启发。自己也尝试了一下,就有了这篇文章。

下面这个方法是从数据库里搜索符合条件的数据(列出由指定用户管理并且已经超过有效期限的文档,以XML形式导出),然后使用指定的xslt文件格式化取得的XML格式数据。

       ///<summary>

       ///生成件内容

       ///</summary>

       ///<param name="date">除日期</param>

       ///<param name="account">管理者ID</param>

       privatestring GetSendMailBoby(DateTime
date,string account)

        {

           using (conn =newSqlConnection(

                      ConfigurationManager.ConnectionStrings["mainDB"].ConnectionString))

            {

                conn.Open();

               string bodyString =string.Empty;

               string commString =string.Empty;

                commString +="SELECT item_name,item_url,parent_url FROM DocAddress ";

                commString +="WHERE manger_id=@account AND due_date<=@executedate AND ";

                commString +="send_flg = 0 FOR XML AUTO, ELEMENTS,ROOT('SendmailToUser')";

               SqlCommand command =newSqlCommand(commString,
conn);

                command.Parameters.AddWithValue("@account", account);

                command.Parameters.AddWithValue("@executedate", date);

               using (XmlReader reader = command.ExecuteXmlReader())

                {

                   XslCompiledTransform transform =new System.Xml.Xsl.XslCompiledTransform();

                    transform.Load(

                       Application.StartupPath +@""" +ConfigurationManager.AppSettings["Mail.Template"]

                        );

                   XsltArgumentList argList =newXsltArgumentList();

                    argList.AddParam("deleteday","", date.AddDays(14).ToLongDateString());

                   using (StringWriter writer =newStringWriter())

                    {

                        transform.Transform(reader, argList, writer);

                        bodyString = writer.ToString();

                    }

                }

                conn.Close();

               return bodyString;

            }

        }

相应的XSLT文件。

<?xmlversion="1.0"encoding="utf-8"
?>

<xsl:stylesheetversion="1.0"

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:outputmethod="html"
/>

<xsl:paramname="deleteday"/>

<xsl:templatematch="DocAddress">

    <tr>

      <tdwidth="250">

        <xsl:elementname="A">

          <xsl:attributename="href">

            <xsl:value-ofselect="item_url"/>

          </xsl:attribute>

          <xsl:value-ofselect="item_name"/>

        </xsl:element>

      </td>

      <td>

        <xsl:elementname="A">

          <xsl:attributename="href">

            <xsl:value-ofselect="parent_url"/>

          </xsl:attribute>

         保存地址

        </xsl:element>

      </td>

    </tr>

</xsl:template>

<xsl:templatematch="/">

    <html>

      <head>

        <title>期文章除通知</title>

      </head>

      <body>

       您所管理的下列件将在<xsl:value-ofselect="$deleteday"/>予以除。

        <br/><br/>

        <tablebordercolor="#000000"border="1"cellspacing="0"
>

          <tr>

            <tdwidth="250"style="font-weight:
bold; background-color: silver
">

              <center>文件名</center>

            </td>

            <tdstyle="font-weight:
bold; background-color: silver
">

              <center>地址</center>

            </td>

          </tr>

          <xsl:apply-templatesselect="/SendmailToUser/DocAddress"
/>

        </table>

        <br/>

      </body>

    </html>

</xsl:template>

</xsl:stylesheet>

具体的xslt语法,参考下列网址

http://www.w3school.com.cn/xsl/index.asp

附上完整代码。(相关数据库表的建立参考压缩包内[~TableCreate.txt]文件。此外,因为涉及到调用MOSS2007的服务器端设定,必须引用Microsoft.SharePoint.dll,请自行添加DLL或删除相关代码。)

/Files/Kenr/MailSender.rar

 

====================

http://www.cnblogs.com/kenr/archive/2009/06/24/1510141.html

 

 

前段时间写了个邮件xml的xsl

http://www.4ucode.com/Study/Topic/67000

 

http://yedragon.iteye.com/blog/795921

 

========================================

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        private XslCompiledTransform GetTemplate()
        {
            //xsl操作学习资料:http://www.w3school.com.cn/xsl/index.asp
            XmlTextReader xmlTextReader = new XmlTextReader(Context.Server.MapPath("cdcatalog.xsl"));
            XslCompiledTransform xct = new XslCompiledTransform();
            xct.Load(xmlTextReader);
            xmlTextReader.Close();
            return xct;
        }

        private XPathDocument GetXmlData()
        {
            XmlTextReader xmlTextReader = new XmlTextReader(Context.Server.MapPath("cdcatalog.xml"));
            //数据规格说明
            XPathDocument xPathDoc = new XPathDocument(xmlTextReader, XmlSpace.Preserve);
            xmlTextReader.Close();
            return xPathDoc;
        }

        private string Transform(XslCompiledTransform xsl, XPathDocument xmlData)
        {
            XPathNavigator xPathNavigator = xmlData.CreateNavigator();
            StringWriter sw = new StringWriter();
            xsl.Transform(xPathNavigator, null, sw);
            string xhtml = sw.ToString();
            sw.Close();
            return xhtml;
        }

        protected void btnTransform_Click(object sender, EventArgs e)
        {
            XslCompiledTransform xsl = this.GetTemplate();
            XPathDocument xmlDoc = this.GetXmlData();
            string xhtml = this.Transform(xsl, xmlDoc);
            this.tbXhtml.Text = xhtml;
        }
    }

 

=======================

cdcatalog.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
 <cd>
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>10.90</price>
  <year>1985</year>
 </cd>
 <cd>
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <country>UK</country>
  <company>CBS Records</company>
  <price>9.90</price>
  <year>1988</year>
 </cd>
 <cd>
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <country>USA</country>
  <company>RCA</company>
  <price>9.90</price>
  <year>1982</year>
 </cd>
 <cd>
  <title>Still got the blues</title>
  <artist>Gary Moore</artist>
  <country>UK</country>
  <company>Virgin records</company>
  <price>10.20</price>
  <year>1990</year>
 </cd>
 <cd>
  <title>Eros</title>
  <artist>Eros Ramazzotti</artist>
  <country>EU</country>
  <company>BMG</company>
  <price>9.90</price>
  <year>1997</year>
 </cd>
 <cd>
  <title>One night only</title>
  <artist>Bee Gees</artist>
  <country>UK</country>
  <company>Polydor</company>
  <price>10.90</price>
  <year>1998</year>
 </cd>
 <cd>
  <title>Sylvias Mother</title>
  <artist>Dr.Hook</artist>
  <country>UK</country>
  <company>CBS</company>
  <price>8.10</price>
  <year>1973</year>
 </cd>
 <cd>
  <title>Maggie May</title>
  <artist>Rod Stewart</artist>
  <country>UK</country>
  <company>Pickwick</company>
  <price>8.50</price>
  <year>1990</year>
 </cd>
 <cd>
  <title>Romanza</title>
  <artist>Andrea Bocelli</artist>
  <country>EU</country>
  <company>Polydor</company>
  <price>10.80</price>
  <year>1996</year>
 </cd>
 <cd>
  <title>When a man loves a woman</title>
  <artist>Percy Sledge</artist>
  <country>USA</country>
  <company>Atlantic</company>
  <price>8.70</price>
  <year>1987</year>
 </cd>
 <cd>
  <title>Black angel</title>
  <artist>Savage Rose</artist>
  <country>EU</country>
  <company>Mega</company>
  <price>10.90</price>
  <year>1995</year>
 </cd>
 <cd>
  <title>1999 Grammy Nominees</title>
  <artist>Many</artist>
  <country>USA</country>
  <company>Grammy</company>
  <price>10.20</price>
  <year>1999</year>
 </cd>
 <cd>
  <title>For the good times</title>
  <artist>Kenny Rogers</artist>
  <country>UK</country>
  <company>Mucik Master</company>
  <price>8.70</price>
  <year>1995</year>
 </cd>
 <cd>
  <title>Big Willie style</title>
  <artist>Will Smith</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>9.90</price>
  <year>1997</year>
 </cd>
 <cd>
  <title>Tupelo Honey</title>
  <artist>Van Morrison</artist>
  <country>UK</country>
  <company>Polydor</company>
  <price>8.20</price>
  <year>1971</year>
 </cd>
 <cd>
  <title>Soulsville</title>
  <artist>Jorn Hoel</artist>
  <country>Norway</country>
  <company>WEA</company>
  <price>7.90</price>
  <year>1996</year>
 </cd>
 <cd>
  <title>The very best of</title>
  <artist>Cat Stevens</artist>
  <country>UK</country>
  <company>Island</company>
  <price>8.90</price>
  <year>1990</year>
 </cd>
 <cd>
  <title>Stop</title>
  <artist>Sam Brown</artist>
  <country>UK</country>
  <company>A and M</company>
  <price>8.90</price>
  <year>1988</year>
 </cd>
 <cd>
  <title>Bridge of Spies</title>
  <artist>T`Pau</artist>
  <country>UK</country>
  <company>Siren</company>
  <price>7.90</price>
  <year>1987</year>
 </cd>
 <cd>
  <title>Private Dancer</title>
  <artist>Tina Turner</artist>
  <country>UK</country>
  <company>Capitol</company>
  <price>8.90</price>
  <year>1983</year>
 </cd>
 <cd>
  <title>Midt om natten</title>
  <artist>Kim Larsen</artist>
  <country>EU</country>
  <company>Medley</company>
  <price>7.80</price>
  <year>1983</year>
 </cd>
 <cd>
  <title>Pavarotti Gala Concert</title>
  <artist>Luciano Pavarotti</artist>
  <country>UK</country>
  <company>DECCA</company>
  <price>9.90</price>
  <year>1991</year>
 </cd>
 <cd>
  <title>The dock of the bay</title>
  <artist>Otis Redding</artist>
  <country>USA</country>
  <company>Atlantic</company>
  <price>7.90</price>
  <year>1987</year>
 </cd>
 <cd>
  <title>Picture book</title>
  <artist>Simply Red</artist>
  <country>EU</country>
  <company>Elektra</company>
  <price>7.20</price>
  <year>1985</year>
 </cd>
 <cd>
  <title>Red</title>
  <artist>The Communards</artist>
  <country>UK</country>
  <company>London</company>
  <price>7.80</price>
  <year>1987</year>
 </cd>
 <cd>
  <title>Unchain my heart</title>
  <artist>Joe Cocker</artist>
  <country>USA</country>
  <company>EMI</company>
  <price>8.20</price>
  <year>1987</year>
 </cd>
</catalog>

 

===============================

cdcatalog.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th align="left">Title</th>
        <th align="left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

 

 

 

邮件模板表(模板内容、模板ID,发送者)

邮件发送表(接收者,模板号,数据)

邮件结果表(状态,发送内容,发送次数)

数据源相关功能:录入、导入、数据库

 

数据与模板之前的关系是xpath,而xpath路径,自已也有一些简单语法

  • XSLT <for-each>
  • XSLT <sort>
  • XSLT <if>
  • XSLT <choose>

     

     

     

     

     

  • 抱歉!评论已关闭.