Example #1
0
 protected static void MailSenderThreadArray(object messageObject)
 {
     try
     {
         MailerConfiguration mconfig = (messageObject as object[])[1] as MailerConfiguration;
         SmtpClient          client  = new SmtpClient(mconfig.Host, mconfig.Port);
         client.Credentials    = new NetworkCredential(mconfig.User, mconfig.Password);
         client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
         if ((messageObject as object[])[0] is MailMessage)
         {
             SendSingleMessage(client, (messageObject as object[])[0] as MailMessage);
         }
         else
         {
             if ((messageObject as object[])[0] is IEnumerable <MailMessage> )
             {
                 foreach (MailMessage message in ((messageObject as object[])[0] as IEnumerable <MailMessage>))
                 {
                     SendSingleMessage(client, message);
                 }
             }
         }
     }
     catch (System.Exception e)
     {
         Console.WriteLine("{1}", e.Message);
     }
 }
Example #2
0
        public static bool SaveSection(string fileName, string name, MailerConfiguration section)
        {
            System.Configuration.ConfigXmlDocument doc = new ConfigXmlDocument();
            try
            {
                doc.Load(fileName);
                XmlNode configNode = doc.ChildNodes[1];
                foreach (XmlNode node in configNode.ChildNodes)
                {
                    if (node.Name == name)
                    {
                        configNode.RemoveChild(node);
                    }
                }

                XmlNode newNode = section.CreateNode(doc, name);
                configNode.AppendChild(newNode);
                doc.Save(fileName);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #3
0
        public Template(string filename, bool is_html, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], mainconfigname);

            FromName  = readConfig.SenderName;
            FromEmail = readConfig.SenderEmail;

            IsHTML = is_html;

            try
            {
                System.IO.TextReader fileReader      = new System.IO.StreamReader(filename);
                StringBuilder        templateBuilder = new StringBuilder("");

                Subject = fileReader.ReadLine();
                Subject.Remove(0, 8);

                string line;
                // Read lines from the file until the end of
                // the file is reached.
                while ((line = fileReader.ReadLine()) != null)
                {
                    templateBuilder.AppendLine(line);
                }

                this.Body = templateBuilder.ToString();
            }
            catch (System.Exception) { }
        }
Example #4
0
        public Template()
        {
            //MailerConfiguration readConfig = (MailerConfiguration)System.Configuration.ConfigurationManager.GetSection("mail");
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], "mail");

            FromName  = readConfig.SenderName;
            FromEmail = readConfig.SenderEmail;
        }
Example #5
0
        public Template(StringBuilder email, string subject, bool IsHTML, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], mainconfigname);

            FromName  = readConfig.SenderName;
            FromEmail = readConfig.SenderEmail;

            Subject = subject;
            Body    = email.ToString();

            this.IsHTML = IsHTML;
        }
Example #6
0
        public Template(StringBuilder email, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], mainconfigname);

            FromName  = readConfig.SenderName;
            FromEmail = readConfig.SenderEmail;

            try
            {
                int len = email.ToString().IndexOf(".");
                Subject = email.ToString().Substring(0, len);
                email.Remove(0, len + 1);
                Body = email.ToString();
            }
            catch (System.Exception) { }
        }
Example #7
0
        static Mailer()
        {
            //MailerConfiguration readConfig = (MailerConfiguration)System.Configuration.ConfigurationManager.GetSection("mail");
            MailerConfiguration readConfig  = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], "mail");
            MailerConfiguration boundConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], "returnpath");

            if (readConfig != null)
            {
                _smtpConfig = readConfig;
            }
            if (boundConfig != null)
            {
                replyTo = new MailAddress(boundConfig.SenderEmail, boundConfig.SenderName);
            }
            else
            {
                replyTo = null;
            }
        }
Example #8
0
 static Mailer()
 {
     //MailerConfiguration readConfig = (MailerConfiguration)System.Configuration.ConfigurationManager.GetSection("mail");            
     MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], "mail");
     MailerConfiguration boundConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], "returnpath");
     
     if (readConfig != null)
     {
         _smtpConfig = readConfig;
     }
     if (boundConfig != null)
     {
         replyTo = new MailAddress(boundConfig.SenderEmail, boundConfig.SenderName);                 
     }
     else
     {
         replyTo = null;
     }
 }
Example #9
0
        public Template(string filename, string directory, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(directory, mainconfigname);

            FromName  = readConfig.SenderName;
            FromEmail = readConfig.SenderEmail;
            try
            {
                System.IO.TextReader fileReader      = new System.IO.StreamReader(filename);
                StringBuilder        templateBuilder = new StringBuilder("");
                Subject = fileReader.ReadLine();
                string line;
                while ((line = fileReader.ReadLine()) != null)
                {
                    templateBuilder.AppendLine(line);
                }
                this.Body = templateBuilder.ToString();
            }
            catch (System.Exception) { }
        }
    public static bool SaveSection(string fileName, string name, MailerConfiguration section)
    {
      System.Configuration.ConfigXmlDocument doc = new ConfigXmlDocument();
      try
      {
        doc.Load(fileName);
        XmlNode configNode = doc.ChildNodes[1];
        foreach (XmlNode node in configNode.ChildNodes)
        {
          if (node.Name == name)
            configNode.RemoveChild(node);
        }

        XmlNode newNode = section.CreateNode(doc, name);
        configNode.AppendChild(newNode);
        doc.Save(fileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
Example #11
0
        public static MailMessage RenderPlainHTMLEmail(string mainconfigname, string subject, string htmlText, string plainText, string email, string nameto, Dictionary <string, string> emailHeaders)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], mainconfigname);
            MailMessage         message    = new MailMessage(new MailAddress(readConfig.SenderEmail, readConfig.SenderName), String.IsNullOrEmpty(nameto) ? new MailAddress(email) : new MailAddress(email, nameto));

            try
            {
                message.Subject = subject;
                message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainText, Encoding.UTF8, MediaTypeNames.Text.Plain));
                message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlText, Encoding.UTF8, MediaTypeNames.Text.Html));
                message.Headers.Add("List-Unsubscribe", "<mailto:" + readConfig.SenderEmail + "?subject=" + email + ">");
                if (emailHeaders != null && emailHeaders.Count > 0)
                {
                    foreach (var nvc in emailHeaders)
                    {
                        message.Headers.Add(nvc.Key, nvc.Value);
                    }
                }
            }
            catch (Exception)
            {
            }
            return(message);
        }
Example #12
0
        object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section)
        {
            MailerConfiguration config = new MailerConfiguration();

            try
            {
                int ConfigMessagesPerSession = Convert.ToInt32(section.Attributes["MessagePerSession"]);
                if (ConfigMessagesPerSession > 0)
                {
                    config.MessagesPerSession = ConfigMessagesPerSession;
                }
            }
            catch (System.Exception) { }

            // Gets the child element names and attributes.
            foreach (XmlNode child in section.ChildNodes)
            {
                if (XmlNodeType.Element == child.NodeType)
                {
                    if (child.LocalName == "host")
                    {
                        foreach (XmlAttribute attribute in child.Attributes)
                        {
                            if (attribute.LocalName == "address")
                            {
                                config.Host = attribute.Value;
                            }
                            else
                            if (attribute.LocalName == "port")
                            {
                                config.Port = Convert.ToInt32(attribute.Value);
                            }
                            else
                            if (attribute.LocalName == "user")
                            {
                                config.User = attribute.Value;
                            }
                            if (attribute.LocalName == "password")
                            {
                                config.Password = attribute.Value;
                            }
                        }
                    }
                    else
                    if (child.LocalName == "message")
                    {
                        foreach (XmlNode node in child.ChildNodes)
                        {
                            if (node.LocalName == "sender")
                            {
                                foreach (XmlNode senderChild in node.ChildNodes)
                                {
                                    if (senderChild.LocalName == "name")
                                    {
                                        config.SenderName = senderChild.InnerText;
                                    }
                                    else
                                    if (senderChild.LocalName == "email")
                                    {
                                        config.SenderEmail = senderChild.InnerText;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(config);
        }
    object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section)
    {
      MailerConfiguration config = new MailerConfiguration();

      try
      {
        int ConfigMessagesPerSession = Convert.ToInt32(section.Attributes["MessagePerSession"]);
        if (ConfigMessagesPerSession > 0)
        {
          config.MessagesPerSession = ConfigMessagesPerSession;
        }
      }
      catch (System.Exception) { }

      // Gets the child element names and attributes.
      foreach (XmlNode child in section.ChildNodes)
      {
        if (XmlNodeType.Element == child.NodeType)
        {
          if (child.LocalName == "host")
          {
            foreach (XmlAttribute attribute in child.Attributes)
            {
              if (attribute.LocalName == "address")
              {
                config.Host = attribute.Value;
              }
              else
                if (attribute.LocalName == "port")
                {
                  config.Port = Convert.ToInt32(attribute.Value);
                }
                else
                  if (attribute.LocalName == "user")
                  {
                    config.User = attribute.Value;
                  }
              if (attribute.LocalName == "password")
              {
                config.Password = attribute.Value;
              }
            }
          }
          else
            if (child.LocalName == "message")
            {
              foreach (XmlNode node in child.ChildNodes)
              {
                if (node.LocalName == "sender")
                {
                  foreach (XmlNode senderChild in node.ChildNodes)
                  {
                    if (senderChild.LocalName == "name")
                    {
                      config.SenderName = senderChild.InnerText;
                    }
                    else
                      if (senderChild.LocalName == "email")
                      {
                        config.SenderEmail = senderChild.InnerText;
                      }
                  }
                }
              }
            }

        }
      }

      return config;
    }
Example #14
0
        public static void Enqueue(MailMessage message, string directory, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(directory, mainconfigname);

            System.Threading.ThreadPool.QueueUserWorkItem(MailSenderThreadArray, (object)(new object[] { message, readConfig }));
        }
Example #15
0
        public static void Enqueue(MailMessage message, string mainconfigname)
        {
            MailerConfiguration readConfig = ConfigurationHandler.GetSection(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["MailConfig"], mainconfigname);

            System.Threading.ThreadPool.QueueUserWorkItem(MailSenderThreadArray, (object)(new object[] { message, readConfig }));
        }