using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint; using System.Text; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO; using System.Net.Mail; using System.Web.Configuration; using System.Configuration; using System.Net; using Microsoft.SharePoint.Administration; namespace CKS.FormsBasedAuthentication { public static class Email { public static bool SendEmail(SPWeb web, string emailTo, string xsltTemplateFile) { return SendEmail(web, emailTo, xsltTemplateFile, (null as IDictionary)); } public static bool SendEmail(SPWeb web, string emailTo, string xslt, IDictionary xslValues) { WriteMessage("xslt - " + xslt); XmlDocument xmlDoc; XPathNavigator xpathNavigator; XslCompiledTransform xslEmailTransform = new XslCompiledTransform(); XsltArgumentList xslArguments; StringBuilder sbEmail; XmlTextWriter xmlWriter; XmlNode xmlNodeTitle; XmlDocument xmlEmail; XsltSettings settings = new XsltSettings(true, true); XmlUrlResolver resolver = new XmlUrlResolver(); string subject = string.Empty; try { try { xslEmailTransform.Load(new XmlTextReader(xslt, XmlNodeType.Document, null), settings, resolver); } catch { try { string xsltpath = xslt.Replace(web.Url, "").Replace("_layouts",@"TEMPLATE/LAYOUTS").Replace("15",""); // string xsltpath = xslt.Replace(web.Url, ""); WriteMessage("xslt - " + xsltpath); string setupPath = SPUtility.GetVersionedGenericSetupPath(xsltpath, 15); //using (XmlReader r = XmlReader.Create(new StreamReader(setupPath, Encoding.GetEncoding("ISO-8859-9")))) using (XmlReader r = XmlReader.Create(new StreamReader(setupPath, Encoding.UTF8))) { xslEmailTransform.Load(r); } } catch(Exception ex) { WriteMessage(ex.ToString()); } } WriteMessage("get xslEmailTransform"); xmlDoc = new XmlDocument(); xmlDoc.AppendChild(xmlDoc.CreateElement("DocumentRoot")); xpathNavigator = xmlDoc.CreateNavigator(); xslArguments = new XsltArgumentList(); if (xslValues != null) { foreach (DictionaryEntry xslEntry in xslValues) { xslArguments.AddExtensionObject(xslEntry.Key.ToString(), xslEntry.Value); } } sbEmail = new StringBuilder(); xmlWriter = new XmlTextWriter(new StringWriter(sbEmail)); xslEmailTransform.Transform(xpathNavigator, xslArguments, xmlWriter); xmlEmail = new XmlDocument(); xmlEmail.LoadXml(sbEmail.ToString()); xmlNodeTitle = xmlEmail.SelectSingleNode("//title"); subject = xmlNodeTitle.InnerText; WriteMessage("Sending email"); WriteMessage("Email Body " + sbEmail.ToString()); return SendEmail(web, emailTo, subject, sbEmail.ToString()); } catch (Exception ex) { WriteMessage(ex.ToString()); Utils.LogError(ex); return false; } } public static bool SendEmail(SPWeb web, string emailTo, string subject, string body) { if (!SPUtility.IsEmailServerSet(web)) { WriteMessage("Email server not set"); return false; } WriteMessage("Email server is set"); MembershipSettings settings = new MembershipSettings(web); StringDictionary parameters = new StringDictionary(); parameters.Add("subject", subject); parameters.Add("to", emailTo); parameters.Add("from", settings.MembershipReplyToEmailAddress); return SPUtility.SendEmail(web, parameters, body); } public static void WriteMessage(string error) { FileStream fs = null; StreamWriter sw = null; try { string _strPath = "C://temp/LogCKS.txt"; // string ColumnNames = "SiteName,PageName,Event/Method,QueryDetails,QueryLog,Date"; if (!File.Exists(_strPath)) { error = error + "\n" + DateTime.Now; } fs = File.Open(_strPath, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); sw.WriteLine(error + "\n"); //sw.Close(); } catch (Exception) { // } finally { if (sw != null) { sw.Close(); } if (fs != null) { fs.Close(); } } } } }