I have written code in C# that should transform an XML with the help of a XSL stylesheet, generate some HTML and save it locally where the XML and XSL are there, then send the HTML as e-mail.
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();
}
}
I am not sure whether the following line saves the html locally or not:
XmlTextWriter Writer = new XmlTextWriter(@"C:\Test\CommitReport.html", null);
I am also getting a new error: “The type or namespace name ‘Mail’ does not exist in the namespace ‘System.Web’ (are you missing an assembly reference?)”
The SmtpClient class is defined in the
System.Net.Mailnamespace, notSystem.Web.Mail. Your code needs some modifications. For example things likeResponse.Write(ex.Message);in a console application hardly make sense. Ensuring proper disposal of disposable resources is also important.So try improving your code a little:
Also make sure that