private void SendMail_Click(object sender, EventArgs e)
        {
            ArrayList mails = new ArrayList();

            NewMailingList.MailList mailstruct = new NewMailingList.MailList();
            DataRow drmail = DatabaseConnection.CreateDataset("SELECT BODY,SUBJECT FROM ML_MAIL WHERE ID=" + int.Parse(ViewState["mailid"].ToString())).Tables[0].Rows[0];


            DataTable dt = new DataTable();

            mailstruct.Email       = MailAddress.Text;
            mailstruct.CompanyName = "";
            mailstruct.Name        = "";
            mailstruct.Surname     = "";
            mailstruct.Address     = "";
            mailstruct.City        = "";
            mailstruct.Province    = "";
            mailstruct.Nation      = "";
            mailstruct.Zip         = "";
            mailstruct.RefID       = "";
            mails.Add(mailstruct);


            PrepareMailFields(mails, this.FromMailAddress.Text, drmail[1].ToString(), drmail[0].ToString());
            StartSend(ViewState["mailid"].ToString());
        }
        private void SpoolMail(string mailMessage)
        {
            string fileName;

            if (ConfigSettings.UseSpoolService)
            {
                fileName = ConfigSettings.MailMailingPath + Guid.NewGuid().ToString() + ".xml";

                XmlTextWriter myXmlTextWriter = new XmlTextWriter(fileName, null);
                myXmlTextWriter.Formatting = Formatting.Indented;
                myXmlTextWriter.WriteStartDocument(false);
                myXmlTextWriter.WriteStartElement("ml");

                myXmlTextWriter.WriteStartElement("message");
                myXmlTextWriter.WriteCData(mailMessage);
                myXmlTextWriter.WriteEndElement();
                myXmlTextWriter.WriteStartElement("mailing");
                int i = 1;
                foreach (Object o in mailsdata)
                {
                    NewMailingList.MailList ob = (NewMailingList.MailList)o;
                    myXmlTextWriter.WriteStartElement("mail");
                    myXmlTextWriter.WriteAttributeString("id", (i++).ToString());
                    myXmlTextWriter.WriteAttributeString("to", ob.Email.Trim(' '));
                    myXmlTextWriter.WriteAttributeString("refid", ob.RefID);
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.CompanyName");
                    myXmlTextWriter.WriteAttributeString("value", ob.CompanyName);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.Name");
                    myXmlTextWriter.WriteAttributeString("value", ob.Name);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.Surname");
                    myXmlTextWriter.WriteAttributeString("value", ob.Surname);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.Address");
                    myXmlTextWriter.WriteAttributeString("value", ob.Address);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.City");
                    myXmlTextWriter.WriteAttributeString("value", ob.City);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.Province");
                    myXmlTextWriter.WriteAttributeString("value", ob.Province);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.Nation");
                    myXmlTextWriter.WriteAttributeString("value", ob.Nation);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteStartElement("field");
                    myXmlTextWriter.WriteAttributeString("name", "Tustena.ZipCode");
                    myXmlTextWriter.WriteAttributeString("value", ob.Zip);
                    myXmlTextWriter.WriteEndElement();
                    myXmlTextWriter.WriteEndElement();
                }
                myXmlTextWriter.WriteEndElement();
                myXmlTextWriter.WriteEndElement();
                myXmlTextWriter.Flush();
                myXmlTextWriter.Close();
                ClientScript.RegisterStartupScript(this.GetType(), "sent", string.Format("<script>alert('{0}');</script>", Root.rm.GetString("Acttxt119")));
            }
            else
            {
                foreach (Object o in mailsdata)
                {
                    NewMailingList.MailList ob = (NewMailingList.MailList)o;
                    string tempMessage;
                    fileName = ConfigSettings.MailSpoolPath + Guid.NewGuid().ToString() + ".spool";

                    tempMessage = mailMessage.Replace("*****@*****.**", ob.Email);
                    tempMessage = tempMessage.Replace("Tustena.RefId", ob.RefID);
                    tempMessage = tempMessage.Replace("Tustena.CompanyName", ob.CompanyName);
                    tempMessage = tempMessage.Replace("Tustena.Name", ob.Name);
                    tempMessage = tempMessage.Replace("Tustena.Surname", ob.Surname);
                    tempMessage = tempMessage.Replace("Tustena.Address", ob.Address);
                    tempMessage = tempMessage.Replace("Tustena.City", ob.City);
                    tempMessage = tempMessage.Replace("Tustena.Province", ob.Province);
                    tempMessage = tempMessage.Replace("Tustena.Nation", ob.Nation);
                    tempMessage = tempMessage.Replace("Tustena.ZipCode", ob.Zip);

                    try
                    {
                        using (StreamWriter tw = new StreamWriter(fileName, false))
                        {
                            tw.Write(tempMessage);
                            ClientScript.RegisterStartupScript(this.GetType(), "sent", string.Format("<script>alert('{0}');</script>", Root.rm.GetString("Acttxt119")));
                        }
                    }
                    catch (Exception ex)
                    {
                        Context.Items["warning"] = "Spooler write error!";
                        G.SendError("[Tustena] Spooler write error!", ex.Message);
                        return;
                    }
                }
            }
        }