//the method sends a "welcome" email after the user hss been activated by admin
        public static void SendWelcomeEmail(string to, string url)
        {
            InitSettings();

            string body = Resources.various.WelcomeEmailBody + "\r\n\r\n" + url;
            string subject = _forum + " - " + Resources.various.WelcomeEmailSubject;

            string[] recipients = new string[1];
            recipients[0] = to;

            AsyncSendMail mailer = new AsyncSendMail(
                    recipients,
                    subject,
                    body);
            mailer.AsyncSend();
        }
        //the method sends notifications of new personal messages
        public static void SendPersonalNotificationEmails(string toUserID, string url)
        {
            InitSettings();

            string body = Resources.various.NewPersonalEmailBody + "\r\n" + url;
            string subject = _forum + " - " + Resources.various.NewPersonalSubject;

            DbConnection cn = Utils.DB.CreateConnection();
            DbCommand cmd = Utils.DB.CreateDBProviderFactory().CreateCommand();
            cmd.Connection = cn;
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumUsers WHERE UserID='" + toUserID + "'";

            cn.Open();
            DbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                string[] recipients = new string[1];
                recipients[0] = dr["Email"].ToString();

                AsyncSendMail mailer = new AsyncSendMail(
                    recipients,
                    subject,
                    body);
                mailer.AsyncSend();
            }
            dr.Close();
            cn.Close();
        }
        //the method sends notifications of new forum TOPICS
        public static void SendNewTopicNotificationEmails(int forumID, string userID, string url)
        {
            InitSettings();

            DbConnection cn = Utils.DB.CreateConnection();
            DbCommand cmd = Utils.DB.CreateDBProviderFactory().CreateCommand();
            cmd.Connection = cn;
            cn.Open();

            //addin subscribers to the recipients list
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumNewTopicSubscriptions INNER JOIN ForumUsers ON ForumNewTopicSubscriptions.UserID = ForumUsers.UserID WHERE ForumUsers.UserID<>'" + userID + "' AND ForumNewTopicSubscriptions.ForumID=" + forumID;
            DbDataReader dr = cmd.ExecuteReader();
            ArrayList recipients = new ArrayList();
            while (dr.Read())
            {
                recipients.Add(dr["Email"].ToString());
            }
            dr.Close();

            //addin moderators to the recipients list
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumModerators INNER JOIN ForumUsers ON ForumModerators.UserID = ForumUsers.UserID WHERE ForumUsers.UserID<>'" + userID + "' AND ForumModerators.ForumID=" + forumID;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                recipients.Add(dr["Email"].ToString());
            }
            dr.Close();

            cmd.CommandText = "SELECT Title FROM Forums WHERE ForumID=" + forumID;
            object res = cmd.ExecuteScalar();

            cn.Close();

            string forumurl = url + Utils.GetForumURL(forumID, res.ToString());
            string body = Resources.various.NewThreadEmailBody + "\r\n" + forumurl;
            string subject = _forum + " - " + Resources.various.NewThreadEmailSubject;

            AsyncSendMail mailer = new AsyncSendMail(
                (string[])recipients.ToArray(typeof(string)),
                subject,
                body);
            mailer.AsyncSend();
        }
        //the method sends notifications of new user registrations to administrators
        public static void SendNewUserRegAdminNotification(string url)
        {
            InitSettings();

            string body = "New user has registered on the forum.\r\n\r\nLink to the user's profile: " + url;
            string subject = _forum + " - new user registration";

            DbConnection cn = Utils.DB.CreateConnection();
            DbCommand cmd = Utils.DB.CreateDBProviderFactory().CreateCommand();
            cmd.Connection = cn;
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumUsers INNER JOIN ForumAdministrators ON ForumUsers.UserID=ForumAdministrators.UserID";

            cn.Open();
            DbDataReader dr = cmd.ExecuteReader();
            ArrayList recipients = new ArrayList();
            while (dr.Read())
            {
                recipients.Add(dr["Email"].ToString());
            }

            if (recipients.Count > 0)
            {
                AsyncSendMail mailer = new AsyncSendMail(
                        (string[])(recipients.ToArray(typeof(string))),
                        subject,
                        body);
                mailer.AsyncSend();
            }
            dr.Close();
            cn.Close();
        }
        //the method sends notifications of new forum MESSAGES
        public static void SendNewMsgNotificationEmails(int topicID, string userID, string url)
        {
            InitSettings();

            DbConnection cn = Utils.DB.CreateConnection();
            DbCommand cmd = Utils.DB.CreateDBProviderFactory().CreateCommand();
            cmd.Connection = cn;
            cn.Open();

            //getting topic info
            int forumID; string subj;
            cmd.CommandText = "SELECT ForumID, Subject FROM ForumTopics WHERE TopicID=" + topicID;
            DbDataReader dr = cmd.ExecuteReader();
            dr.Read();
            forumID = Convert.ToInt32(dr["ForumID"]);
            subj = dr["Subject"].ToString();
            dr.Close();

            //addin subscribers to the recipients list
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumSubscriptions INNER JOIN ForumUsers ON ForumSubscriptions.UserID = ForumUsers.UserID WHERE ForumUsers.UserID<>'" + userID + "' AND ForumSubscriptions.TopicID=" + topicID;
            dr = cmd.ExecuteReader();
            ArrayList recipients = new ArrayList();
            while (dr.Read())
            {
                recipients.Add(dr["Email"].ToString());
            }
            dr.Close();

            //addin moderators to the recipients list
            cmd.CommandText = "SELECT ForumUsers.Email FROM ForumModerators INNER JOIN ForumUsers ON ForumModerators.UserID = ForumUsers.UserID WHERE ForumUsers.UserID<>'" + userID + "' AND ForumModerators.ForumID=" + forumID;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                recipients.Add(dr["Email"].ToString());
            }
            dr.Close();

            cn.Close();

            string topicurl = url + Utils.GetTopicURL(topicID, subj);

            //adding "?lastpage=1" to the url
            if (topicurl.IndexOf("?") > -1) topicurl += "&lastpage=1";
            else topicurl += "?lastpage=1";

            string body = Resources.various.ThreadUpdatedEmailBody + "\r\n" + topicurl;
            string subject = _forum + " - " + Resources.various.ThreadUpdatedEmailSubject;

            AsyncSendMail mailer = new AsyncSendMail(
                (string[])recipients.ToArray(typeof(string)),
                subject,
                body);
            mailer.AsyncSend();
        }