生成HTML并将其作为电子邮件发送

时间:2011-11-02 05:32:51

标签: c# .net xml smtp

我已经在C#中编写了代码,它应该在XSL样式表的帮助下转换XML,生成一些HTML并将其保存在XML和XSL所在的本地,然后将HTML作为电子邮件发送。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mail;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class SendMail
{
   static void Main(string[] args)
   {
     {       
       try{

         //load the Xml doc
         XPathDocument XPathDoc = new XPathDocument(@"C:\Test\svnlog.xml") ;

         XslTransform XslTrans = new XslTransform() ;

         //load the Xsl 
         XslTrans.Load(@"C:\Test\svnlog.xsl") ;

         //create the output stream
         XmlTextWriter Writer = new XmlTextWriter(@"C:\Test\CommitReport.html", null);

        //do the actual transform of Xml
        XslTrans.Transform(XPathDoc,null, Writer);        

        Writer.Close() ;

          }
      catch(Exception ex)
    {

        Response.Write(ex.Message);
    }


  using (StreamReader reader = File.OpenText(@"C:\Test\CommitReport.html"))
{                                                         
   MailMessage Mail = new MailMessage();
   Mail.To = ("pqr@dna.com "); 
   Mail.From = new MailAddress("abc@bac.com");
   Mail.Subject = ("Commit Error Report"); 
   Mail.IsBodyHtml = true;  //defines that your email is in Html form 
   Mail.BodyFormat = (@"C:\Test\CommitReport.html"); 

  Mail.Body = reader.ReadToEnd(); 
}

 //create instance of smtpclient 
 SmtpClient smtp = new SmtpClient(); 
 smtp.EnableSsl = true; 
 smtp.Send(mail);

 } 

 }

  private static void MailAddress(string p)
{
        throw new NotImplementedException();
}

 }

我不确定以下行是否在本地保存html:

XmlTextWriter Writer = new XmlTextWriter(@"C:\Test\CommitReport.html", null); 

我也遇到了一个新错误:“名称空间'System.Web'中不存在类型或命名空间名称'Mail'(你是否缺少程序集引用?)”

1 个答案:

答案 0 :(得分:1)

SmtpClient类在System.Net.Mail命名空间中定义,而不是System.Web.Mail。您的代码需要一些修改。例如,控制台应用程序中的Response.Write(ex.Message);之类的东西几乎没有意义。确保妥善处置一次性资源也很重要。

所以尝试改进你的代码:

using System;
using System.IO;
using System.Net.Mail;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

class Program
{
    static void Main()
    {
        try
        {
            var xPathDoc = new XPathDocument(@"C:\Test\svnlog.xml");
            var xslTrans = new XslCompiledTransform();
            xslTrans.Load(@"C:\Test\svnlog.xsl");
            using (var writer = XmlWriter.Create(@"C:\Test\CommitReport.html"))
            {
                xslTrans.Transform(xPathDoc, null, writer);
            }

            var mail = new MailMessage();
            mail.To.Add(new MailAddress("pqr@dna.com"));
            mail.From = new MailAddress("abc@bac.com");
            mail.Subject = "Commit Error Report";
            mail.IsBodyHtml = true;
            mail.Body = File.ReadAllText(@"C:\Test\CommitReport.html");

            using (var smtpClient = new SmtpClient("smtp.yourhost.com"))
            {
                smtpClient.EnableSsl = true;
                smtpClient.Send(mail);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

同时确保