private void SaveToDB(qCom_TempBulkEmailLog t_log, int email_item_id, string email)
        {
            int curr_log_id = 0;
            int i           = 0;

            if ((email == null) || (email.Trim().Length == 0))
            {
                return;
            }

            var logs = qCom_EmailLog.GetEmailLogsByEmailItemIDANDEmail(email_item_id, email);

            if (logs != null)
            {
                foreach (var l in logs)
                {
                    if (i == 0)
                    {
                        curr_log_id = l.EmailLogID;
                    }
                    i++;
                }
            }
            else
            {
                // log doesn't exist so create one
                qCom_EmailItem email_sent = new qCom_EmailItem(email_item_id);
                qCom_EmailLog  log        = new qCom_EmailLog();
                log.ScopeID      = 1;
                log.EmailItemID  = email_item_id;
                log.UserID       = 0;
                log.EmailAddress = email;
                log.Subject      = email_sent.Subject;
                log.Timestamp    = t_log.Timestamp;
                log.Insert();

                curr_log_id = log.EmailLogID;
            }

            if (curr_log_id > 0)
            {
                qCom_EmailLog e_log = new qCom_EmailLog(curr_log_id);
                if (e_log.EmailLogID > 0)
                {
                    e_log.ReadTime = DateTime.Now;
                    e_log.Update();
                }
            }
        }
        public void GetImage_BeginRequest(object sender, System.EventArgs args)
        {
            //cast the sender to a HttpApplication object
            System.Web.HttpApplication application = (System.Web.HttpApplication)sender;

            string url = application.Request.Path;             //get the url path

            //create the regex to match for becon images
            Regex r = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            if (r.IsMatch(url))
            {
                string full_url = application.Request.RawUrl;

                if (full_url.Contains("?id="))
                {
                    int    start_qs      = full_url.IndexOf("?id=");
                    int    email_item_id = 0;
                    string email         = string.Empty;
                    int    length_qs     = full_url.Length;
                    string raw_id        = full_url.Substring(start_qs + 4, length_qs - (start_qs + 4));

                    if (!String.IsNullOrEmpty(raw_id))
                    {
                        int t_log_id = Convert.ToInt32(raw_id);
                        qCom_TempBulkEmailLog t_log = new qCom_TempBulkEmailLog(t_log_id);
                        email_item_id = t_log.EmailItemID;
                        email         = t_log.EmailAddress;

                        try
                        {
                            SaveToDB(t_log, email_item_id, email);
                        }
                        catch
                        {
                            // do nothing
                        }
                    }
                }

                //now send the image to the client
                application.Response.ContentType = "image/gif";
                application.Response.WriteFile(application.Request.MapPath(spacerFile));

                //end the resposne
                application.Response.End();
            }
        }
        public void GetImage_BeginRequest(object sender, System.EventArgs args)
        {
            //cast the sender to a HttpApplication object
            System.Web.HttpApplication application =(System.Web.HttpApplication)sender;

            string url = application.Request.Path; //get the url path

            //create the regex to match for becon images
            Regex r =new Regex( pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase );
            if( r.IsMatch( url ) )
            {
                string full_url = application.Request.RawUrl;

                if (full_url.Contains("?id="))
                {
                    int start_qs = full_url.IndexOf("?id=");
                    int email_item_id = 0;
                    string email = string.Empty;
                    int length_qs = full_url.Length;
                    string raw_id = full_url.Substring(start_qs + 4, length_qs - (start_qs + 4));

                    if (!String.IsNullOrEmpty(raw_id))
                    {
                        int t_log_id = Convert.ToInt32(raw_id);
                        qCom_TempBulkEmailLog t_log = new qCom_TempBulkEmailLog(t_log_id);
                        email_item_id = t_log.EmailItemID;
                        email = t_log.EmailAddress;

                        try
                        {
                            SaveToDB(t_log, email_item_id, email);
                        }
                        catch
                        {
                            // do nothing
                        }
                    }
                }

                //now send the image to the client
                application.Response.ContentType = "image/gif";
                application.Response.WriteFile(  application.Request.MapPath( spacerFile ) );

                //end the resposne
                application.Response.End();
            }
        }
        private void SaveToDB(qCom_TempBulkEmailLog t_log, int email_item_id, string email)
        {
            int curr_log_id = 0;
            int i = 0;
            if( ( email==null) || ( email.Trim().Length == 0 ) )
                return;

            var logs = qCom_EmailLog.GetEmailLogsByEmailItemIDANDEmail(email_item_id, email);

            if (logs != null)
            {
                foreach (var l in logs)
                {
                    if (i == 0)
                        curr_log_id = l.EmailLogID;
                    i++;
                }
            }
            else
            {
                // log doesn't exist so create one
                qCom_EmailItem email_sent = new qCom_EmailItem(email_item_id);
                qCom_EmailLog log = new qCom_EmailLog();
                log.ScopeID = 1;
                log.EmailItemID = email_item_id;
                log.UserID = 0;
                log.EmailAddress = email;
                log.Subject = email_sent.Subject;
                log.Timestamp = t_log.Timestamp;
                log.Insert();

                curr_log_id = log.EmailLogID;

            }

            if (curr_log_id > 0)
            {
                qCom_EmailLog e_log = new qCom_EmailLog(curr_log_id);
                if (e_log.EmailLogID > 0)
                {
                    e_log.ReadTime = DateTime.Now;
                    e_log.Update();
                }
            }
        }
        //Send to just one address with text replacement
        public int SendDatabaseMail(string address, int emailID, int userID, string userName, string value1, string value2, string value3, string value4, bool no_duplicates_today)
        {
            // get email info
            qCom_EmailTool email      = new qCom_EmailTool(emailID);
            qCom_EmailItem full_email = new qCom_EmailItem(emailID);

            string header              = string.Empty;
            string footer              = string.Empty;
            string strFromEmail        = string.Empty;
            string fromName            = string.Empty;
            string emailUsername       = null;
            string emailPassword       = null;
            string emailServer         = null;
            bool   sslEnabled          = false;
            int    portNum             = 0;
            string strDID              = string.Empty;
            int    return_email_log_id = 0;
            bool   passed_send_checks  = false;
            bool   already_sent_today  = true;
            string returnedEmail       = string.Empty;
            bool   ok_to_send          = false;
            string noEmail             = string.Empty;


            // Check to see if email already sent to user today
            if (no_duplicates_today == true)
            {
                var prior_current_day_log = qCom_EmailLog.CheckForEmailSentLastDay(address);

                if (prior_current_day_log == null)
                {
                    already_sent_today = false;
                }
            }
            else
            {
                already_sent_today = false;
            }

            qCom_UserPreference_View u_pref = new qCom_UserPreference_View(address);

            if (u_pref.UserID > 0)
            {
                if (u_pref.OkBulkEmail == "Yes" || u_pref.OkEmail == "Yes")
                {
                    ok_to_send = true;
                }
            }
            else
            {
                ok_to_send = true;
            }

            if (already_sent_today == false && ok_to_send == true)
            {
                passed_send_checks = true;
            }
            else
            {
                passed_send_checks = false;
            }

            qPtl_User user = new qPtl_User(Convert.ToInt32(userID));

            if (user.UserID == 0)
            {
                user.Email     = "";
                user.FirstName = "";
                user.LastName  = "";
            }

            // load default email preferences
            if (String.IsNullOrEmpty(emailServer))
            {
                var email_pref = qCom_EmailPreference.GetEmailPreferences();

                if (email_pref != null)
                {
                    if (email_pref.EmailPreferencesID > 0)
                    {
                        header        = email_pref.Header;
                        footer        = email_pref.Footer;
                        strFromEmail  = email_pref.FromEmailAddress;
                        fromName      = email_pref.FromName;
                        emailServer   = email_pref.SMTPServer;
                        emailUsername = email_pref.SMTPUsername;
                        emailPassword = email_pref.SMTPPassword;
                        sslEnabled    = email_pref.SMTPSSL;
                        portNum       = email_pref.SMTPPort;
                    }
                }
            }

            // format as friendly send address
            string strFullEmail = string.Empty;

            if (!String.IsNullOrEmpty(fromName))
            {
                strFullEmail = fromName + " <" + strFromEmail + ">";
            }
            else
            {
                strFullEmail = strFromEmail;
            }

            string messageBody = header;

            messageBody += email.emailDraft;
            emailSubject = email.emailSubject;

            messageBody += footer;

            if (!String.IsNullOrEmpty(emailServer) && passed_send_checks == true)
            {
                SmtpClient emailClient = new SmtpClient(emailServer, portNum);
                emailClient.EnableSsl      = sslEnabled;
                emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                emailClient.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);

                Regex emailCheck = new Regex("^.*<?[-a-zA-Z0-9!#$%&'*+/=?^_`{|}~]+(\\.[-a-zA-Z0-9!#$%&'*+/=?^_`{|}~]+)*@(([a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){1,63}\\.)+([a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){2,63}>?$");

                // create temp bulk email log -- used for tracking (must be created before sent so can include id in message html)
                int t_log_id = 0;
                qCom_TempBulkEmailLog t_log = new qCom_TempBulkEmailLog();
                t_log.EmailItemID  = emailID;
                t_log.EmailAddress = strFullEmail;
                t_log.Timestamp    = DateTime.Now;
                t_log.Insert();
                t_log_id = t_log.TempBulkEmailLogID;

                if (emailCheck.IsMatch(address.ToLower()))
                {
                    string finalBody = parseContentField(new string[] { Convert.ToString(user.UserID), userName, user.FirstName, user.LastName, user.Email, DateTime.Now.ToString(), value1, value2, value3, value4, strDID, Convert.ToString(emailID), Convert.ToString(t_log_id) }, messageBody);

                    string finalSubject = parseContentField(new string[] { Convert.ToString(user.UserID), userName, user.FirstName, user.LastName, user.Email, DateTime.Now.ToString(), value1, value2, value3, value4, strDID, Convert.ToString(emailID), Convert.ToString(t_log_id) }, emailSubject);

                    MailMessage mailout = new MailMessage(strFullEmail, address, finalSubject, finalBody);
                    mailout.IsBodyHtml = true;

                    emailClient.Send(mailout);

                    return_email_log_id = AddTransmissionLog(scopeID, address, userID, "single", finalSubject, emailID, full_email.CampaignID, strFromEmail);
                }
            }

            return(return_email_log_id);
        }
        //Send to just one address with text replacement
        public int SendDatabaseMail(string address, int emailID, int userID, string userName, string value1, string value2, string value3, string value4, bool no_duplicates_today)
        {
            // get email info
            qCom_EmailTool email = new qCom_EmailTool(emailID);
            qCom_EmailItem full_email = new qCom_EmailItem(emailID);

            string header = string.Empty;
            string footer = string.Empty;
            string strFromEmail = string.Empty;
            string fromName = string.Empty;
            string emailUsername = null;
            string emailPassword = null;
            string emailServer = null;
            bool sslEnabled = false;
            int portNum = 0;
            string strDID = string.Empty;
            int return_email_log_id = 0;
            bool passed_send_checks = false;
            bool already_sent_today = true;
            string returnedEmail = string.Empty;
            bool ok_to_send = false;
            string noEmail = string.Empty;

            // Check to see if email already sent to user today
            if (no_duplicates_today == true)
            {
                var prior_current_day_log = qCom_EmailLog.CheckForEmailSentLastDay(address);

                if (prior_current_day_log == null)
                {
                    already_sent_today = false;
                }
            }
            else
            {
                already_sent_today = false;
            }

            qCom_UserPreference_View u_pref = new qCom_UserPreference_View(address);
            if (u_pref.UserID > 0)
            {
                if (u_pref.OkBulkEmail == "Yes" || u_pref.OkEmail == "Yes")
                {
                    ok_to_send = true;
                }
            }
            else
                ok_to_send = true;

            if (already_sent_today == false && ok_to_send == true)
                passed_send_checks = true;
            else
                passed_send_checks = false;

            qPtl_User user = new qPtl_User(Convert.ToInt32(userID));
            if (user.UserID == 0)
            {
                user.Email = "";
                user.FirstName = "";
                user.LastName = "";
            }

            // load default email preferences
            if (String.IsNullOrEmpty(emailServer))
            {
                var email_pref = qCom_EmailPreference.GetEmailPreferences();

                if (email_pref != null)
                {
                    if (email_pref.EmailPreferencesID > 0)
                    {
                        header = email_pref.Header;
                        footer = email_pref.Footer;
                        strFromEmail = email_pref.FromEmailAddress;
                        fromName = email_pref.FromName;
                        emailServer = email_pref.SMTPServer;
                        emailUsername = email_pref.SMTPUsername;
                        emailPassword = email_pref.SMTPPassword;
                        sslEnabled = email_pref.SMTPSSL;
                        portNum = email_pref.SMTPPort;
                    }
                }
            }

            // format as friendly send address
            string strFullEmail = string.Empty;
            if (!String.IsNullOrEmpty(fromName))
                strFullEmail = fromName + " <" + strFromEmail + ">";
            else
                strFullEmail = strFromEmail;

            string messageBody = header;

            messageBody += email.emailDraft;
            emailSubject = email.emailSubject;

            messageBody += footer;

            if (!String.IsNullOrEmpty(emailServer) && passed_send_checks == true)
            {
                SmtpClient emailClient = new SmtpClient(emailServer, portNum);
                emailClient.EnableSsl = sslEnabled;
                emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                emailClient.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);

                Regex emailCheck = new Regex("^.*<?[-a-zA-Z0-9!#$%&'*+/=?^_`{|}~]+(\\.[-a-zA-Z0-9!#$%&'*+/=?^_`{|}~]+)*@(([a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){1,63}\\.)+([a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){2,63}>?$");

                // create temp bulk email log -- used for tracking (must be created before sent so can include id in message html)
                int t_log_id = 0;
                qCom_TempBulkEmailLog t_log = new qCom_TempBulkEmailLog();
                t_log.EmailItemID = emailID;
                t_log.EmailAddress = strFullEmail;
                t_log.Timestamp = DateTime.Now;
                t_log.Insert();
                t_log_id = t_log.TempBulkEmailLogID;

                if (emailCheck.IsMatch(address.ToLower()))
                {
                    string finalBody = parseContentField(new string[] { Convert.ToString(user.UserID), userName, user.FirstName, user.LastName, user.Email, DateTime.Now.ToString(), value1, value2, value3, value4, strDID, Convert.ToString(emailID), Convert.ToString(t_log_id) }, messageBody);

                    string finalSubject = parseContentField(new string[] { Convert.ToString(user.UserID), userName, user.FirstName, user.LastName, user.Email, DateTime.Now.ToString(), value1, value2, value3, value4, strDID, Convert.ToString(emailID), Convert.ToString(t_log_id) }, emailSubject);

                    MailMessage mailout = new MailMessage(strFullEmail, address, finalSubject, finalBody);
                    mailout.IsBodyHtml = true;

                    emailClient.Send(mailout);

                    return_email_log_id = AddTransmissionLog(scopeID, address, userID, "single", finalSubject, emailID, full_email.CampaignID, strFromEmail);
                }
            }

            return return_email_log_id;
        }