2011-11-02 81 views
0

我已經用C#編寫代碼,它應該藉助於XSL樣式表來轉換XML,生成一些HTML並將其保存在XML和XSL所在的位置,然後將HTML作爲電子郵件。生成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 = ("[email protected] "); 
    Mail.From = new MailAddress("[email protected]"); 
    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'(您是否缺少程序集引用?)「

+0

你有沒有在C檢查:\如果CommitReport.html文件被創建測試\? –

+0

是啊......它沒有生成......語法正確嗎? – dibya

+0

你回來什麼錯誤。 Mail.BodyFormat屬性不是一個字符串。 http://msdn.microsoft.com/en-us/library/system.web.mail.mailmessage.bodyformat.aspx – Kieran

回答

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("[email protected]")); 
      mail.From = new MailAddress("[email protected]"); 
      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); 
     } 
    } 
} 

還要確保