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'(您是否缺少程序集引用?)「
你有沒有在C檢查:\如果CommitReport.html文件被創建測試\? –
是啊......它沒有生成......語法正確嗎? – dibya
你回來什麼錯誤。 Mail.BodyFormat屬性不是一個字符串。 http://msdn.microsoft.com/en-us/library/system.web.mail.mailmessage.bodyformat.aspx – Kieran