/// <summary> /// Creates the client /// </summary> /// <param name="SetFileName">Sets the File Name (or path from excutable with file name) Default = \\ServerOptions.config</param> /// <param name="SetDefaultEncoding">Sets the default message encoder to use with message Default = UTF8Encoding.UTF8</param> /// <param name="EnableSsl">Are you Using SSL(should be)</param> /// <param name="CompleteEvents">Do you want a call back?or 2(prob not as one is more thhan enough)</param> public SMTPClient(string SetFileName, Encoding SetDefaultEncoding = null, bool EnableSsl = true, SendCompletedEventHandler[] CompleteEvents = null) { //if default encoding is not null re set it DefaultEncoding = SetDefaultEncoding ?? UTF8Encoding.UTF8; //make an options loader and load into it from an xml SMTPClientLoadOpt Opts = null; using (XmlReader Loader = XmlReader.Create(File.OpenRead(SetFileName))) { // Read The File Till we find the node or the end of the file try { Opts = Loader.ReadContentAsSMTPClientLoadOpt(); } catch (XmlException e) { Loader.Close(); //ensure a file close any way throw e; // and throw it out again } catch (Exception e) { Loader.Close(); //ensure a file close any way throw e; // and throw it out again } Loader.Close(); } // of we made it to the end of the file with out making a Options thats a problem if (Opts == null) { throw new Exception("SMTPClientLoadOpt node not found before end of file"); } DefaultFrom = Opts.DefaultFrom; // make a client with all the settings Client = new SmtpClient(Opts.Host, Opts.Port.Value); Client.Credentials = new NetworkCredential(Opts.Account, Opts.Password); Client.EnableSsl = EnableSsl; if (CompleteEvents != null) { foreach (SendCompletedEventHandler SCE in CompleteEvents) { Client.SendCompleted += SCE; } } }
public static SMTPClientLoadOpt ReadContentAsSMTPClientLoadOpt(this XmlReader Loader) { SMTPClientLoadOpt Return = null; bool InIt = false; do { switch (Loader.NodeType) { //look for all the nodes and grab them case XmlNodeType.Element: switch (Loader.Name) { case "SMTPClientLoadOpt": if (Return != null) // to avoid Ambugis throw if we've seen one { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes.", new Exception("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return = new SMTPClientLoadOpt(); InIt = true; break; case "Host": if (InIt) //if we are not in the the node skip : avoid Ambugis { if (Return.Host != null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("Host already found making this line ambiguous.", new Exception("Host already found making this line ambiguous."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return.Host = Loader.ReadElementContentAsString(); } break; case "Port": if (InIt) { if (Return.Port != null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("Port already found making this line ambiguous.", new Exception("Port already found making this line ambiguous."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return.Port = Loader.ReadElementContentAsInt(); } break; case "DefaultFrom": if (InIt) { if (Return.DefaultFrom != null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("DefaultFrom already found making this line ambiguous.", new Exception("DefaultFrom already found making this line ambiguous."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return.DefaultFrom = new MailAddress(Loader.ReadElementContentAsString()); } break; case "Account": if (InIt) { if (Return.Account != null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("Account already found making this line ambiguous.", new Exception("Account already found making this line ambiguous."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return.Account = Loader.ReadElementContentAsString(); } break; case "Password": if (InIt) { if (Return.Password != null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("Password already found making this line ambiguous.", new Exception("Password already found making this line ambiguous."), xmlInfo.LineNumber, xmlInfo.LinePosition); } Return.Password = Loader.ReadElementContentAsString(); } break; default: if (InIt) // to avoid bad shit kill it if we find a bad node and in it { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("Unexpected node found.", new Exception("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes."), xmlInfo.LineNumber, xmlInfo.LinePosition); } break; } break; //if we find the end node do some checkes case XmlNodeType.EndElement: if (Loader.Name == "SMTPClientLoadOpt") { //throw if we find the end first. if (Return == null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("SMTPClientLoadOpt end found before start", new Exception("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes."), xmlInfo.LineNumber, xmlInfo.LinePosition); } //Throw on incomplete data else if (Return.Account == null || Return.DefaultFrom == null || Return.Host == null || Return.Password == null || Return.Port == null) { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; string returnMSG = ""; returnMSG += Return.Account == null ? " Account " : ""; returnMSG += Return.DefaultFrom == null ? " DefaultFrom " : ""; returnMSG += Return.Host == null ? " Host " : ""; returnMSG += Return.Password == null ? " Password " : ""; returnMSG += Return.Port == null ? " Port " : ""; returnMSG += "nodes were not found in SMTPClientLoadOpt before its end"; throw new XmlException(returnMSG, new Exception("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes."), xmlInfo.LineNumber, xmlInfo.LinePosition); } else if (InIt) { InIt = false; } } break; } }while (Loader.Read()); if (!InIt && Return != null) { return(Return); } { IXmlLineInfo xmlInfo = Loader as IXmlLineInfo; throw new XmlException("unexpected end config file found before end of SMTPClientLoadOpt", new Exception("SMTPClientLoadOpt found already. File can not contain mulitple SMTPClientLoadOpt nodes."), xmlInfo.LineNumber, xmlInfo.LinePosition); } }