/// <summary>
        /// Δημιουργεί ένα νέο HttpCookie που λήγει σε 28 ημέρες!
        /// <para>Αυτό το cookie φέρει το κλειδί του Collector για το οποίο εκδίδεται, και το κλειδί το Recipient για
        /// τον οποίο παράχθηκε</para>
        /// <para></para>
        /// </summary>
        /// <param name="collector"></param>
        /// <param name="recipientWebKey"></param>
        /// <returns></returns>
        HttpCookie CreateWebKeyCookie(VLCollector collector, string recipientWebKey)
        {
            HttpCookie surveyCookie = new HttpCookie("Collector$" + collector.WebLink);

            surveyCookie.Values.Add("RecipientWebKey", recipientWebKey);
            surveyCookie.Expires = DateTime.UtcNow.AddDays(28);
            return(surveyCookie);
        }
        /// <summary>
        /// Επιστρέφει ένα URL για την αφαίρεση του συγκεκριμένου recipient απο την address list του συγκεκριμένου πελάτη που τρέχει το survey.
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="message"></param>
        /// <param name="recipient"></param>
        /// <returns></returns>
        internal static string GetRemoveRecipientURL(VLSurvey survey, VLCollector collector, VLMessage message, VLRecipient recipient)
        {
            var host = ValisSystem.Settings.Core.SystemPublicHostName;
            var _url = ValisSystem.Settings.Core.RemoveUrl.Url;

            var url = string.Format(@"{0}?rkey={1}&pid={2}&cid={3}&lang={4}", _url, recipient.RecipientKey, survey.PublicId, collector.CollectorId, BuiltinLanguages.PrimaryLanguage.LanguageId);

            url = string.Format("{0}/{1}", host, url).Replace("//", "/");
            return(string.Format("http://{0}", url));
        }
        string GetRecipientWebKeyFromCookies(VLCollector collector, HttpRequest request)
        {
            HttpCookie surveyCookie = request.Cookies["Collector$" + collector.WebLink];

            if (surveyCookie != null)
            {
                return(surveyCookie.Values["RecipientWebKey"].ToString());
            }
            return(null);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="recipient"></param>
        /// <param name="manualEntry"></param>
        /// <returns></returns>
        internal static string GetSurveyRuntimeURL(VLSurvey survey, VLCollector collector, VLRecipient recipient, bool manualEntry = false)
        {
            var host         = ValisSystem.Settings.Core.RuntimeEngine.Host;
            var absolutePath = GetSurveyRuntimeAbsolutePath(survey, collector, recipient, manualEntry);

            var url = string.Format(@"{0}{1}", host, absolutePath);

            if (collector.UseSSL)
            {
                return(string.Format("https://{0}", url));
            }
            else
            {
                return(string.Format("http://{0}", url));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="recipient"></param>
        /// <param name="manualEntry"></param>
        /// <returns></returns>
        internal static string GetSurveyRuntimeAbsolutePath(VLSurvey survey, VLCollector collector, VLRecipient recipient, bool manualEntry)
        {
            if (collector.CollectorType != CollectorType.Email)
            {
                throw new VLException(string.Format("Collector '{0}', has wrong type!", collector.Name));
            }

            if (manualEntry == false)
            {
                var absolutePath = string.Format(@"/em/{0}/{1}/{2}/{3}/", survey.PublicId, recipient.RecipientKey, collector.CollectorId, BuiltinLanguages.GetTwoLetterISOCode(collector.TextsLanguage));
                return(absolutePath);
            }
            else
            {
                var absolutePath = string.Format(@"/emm/{0}/{1}/{2}/{3}/", survey.PublicId, recipient.RecipientKey, collector.CollectorId, BuiltinLanguages.GetTwoLetterISOCode(collector.TextsLanguage));
                return(absolutePath);
            }
        }
        /// <summary>
        /// Επιστρέφει το absolutepath του συγκεκριμένου συλλέκτη/survey.
        /// <para>O συλλέκτης πρέπει να είναι τύπου WebLink</para>
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="manualEntry"></param>
        /// <returns></returns>
        internal static string GetSurveyRuntimeAbsolutePath(VLSurvey survey, VLCollector collector, bool manualEntry)
        {
            if (collector.CollectorType != CollectorType.WebLink)
            {
                throw new VLException(string.Format("Collector '{0}', has wrong type!", collector.Name));
            }

            if (manualEntry == false)
            {
                var absolutePath = string.Format(@"/w/{0}/{1}/", collector.WebLink, BuiltinLanguages.GetTwoLetterISOCode(collector.TextsLanguage));
                return(absolutePath);
            }
            else
            {
                var absolutePath = string.Format(@"/wm/{0}/{1}/", collector.WebLink, BuiltinLanguages.GetTwoLetterISOCode(collector.TextsLanguage));
                return(absolutePath);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="message"></param>
        /// <param name="recipient"></param>
        /// <returns></returns>
        internal static string GetRemoveRecipientLink(VLSurvey survey, VLCollector collector, VLMessage message, VLRecipient recipient)
        {
            var url = GetRemoveRecipientURL(survey, collector, message, recipient);

            return(string.Format("<a href=\"{0}\">{0}</a>", url));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="collector"></param>
        /// <param name="recipient"></param>
        /// <param name="manualEntry"></param>
        /// <returns></returns>
        internal static string GetSurveyRuntimeLink(VLSurvey survey, VLCollector collector, VLRecipient recipient, bool manualEntry = false)
        {
            var url = GetSurveyRuntimeURL(survey, collector, recipient, manualEntry);

            return(string.Format("<a href=\"{0}\">{1}</a>", url, WebUtility.HtmlEncode(url)));
        }
Example #9
0
        void SendMails(VLSurvey survey, VLCollector collector, VLMessage message)
        {
            IEmailProvider provider = GetProvider();
            int            totalRows = 0, pageIndex = 1, pageSize = 20;

            var fromDisplayName = SystemManager.GetSystemParameterByKey("@FromDisplayName");

            if (fromDisplayName == null)
            {
                throw new VLException("SystemParameters: @FromDisplayName undefined");
            }

            InfoFormat("TheMailler({0})::SendMails(), called for messageid={1} & subject={2}", this.Name, message.MessageId, message.Subject);


            /*διαβάζουμε σελίδα - σελίδα όλα τα messageRecipients για αυτό το message:*/
            var recipients = SurveyManager.GetRecipientsForMessage(message, pageIndex++, pageSize, ref totalRows);

            while (recipients.Count > 0)
            {
                foreach (var recipient in recipients)
                {
                    #region loop each recipient and send the email:

                    //Εχουμε έναν Recipient:
                    VLMessageRecipient messageRecipient = null;
                    try
                    {
                        messageRecipient = SurveyManager.GetMessageRecipientById(message.MessageId, recipient.RecipientId);
                        if (messageRecipient == null)
                        {
                            throw new VLException(string.Format("There is no MessageRecipient for messageId={0} and recipient!d={1}", message.MessageId, recipient.RecipientId));
                        }
                        messageRecipient.SendDT = Utility.UtcNow();

                        /*Οσα Recipients έχουν γίνει OptedOut δεν τα στέλνουμε:*/
                        if (recipient.IsOptedOut)
                        {
                            DebugFormat("Sending email to {0} (messageId = {1}, recipient = {2})-> FAILED (IsOptedOut)", recipient.Email, message.MessageId, recipient.RecipientId);
                            message.SkipCounter++;
                            messageRecipient.ErrorCount++;
                            messageRecipient.Status = MessageRecipientStatus.OptedOut;
                            continue;
                        }
                        /*Οσα Recipients έχουν γίνει Bounced, δεν τα στέλνουμε:*/
                        if (recipient.IsBouncedEmail)
                        {
                            DebugFormat("Sending email to {0} (messageId = {1}, recipient = {2})-> FAILED (IsBouncedEmail)", recipient.Email, message.MessageId, recipient.RecipientId);
                            message.SkipCounter++;
                            messageRecipient.ErrorCount++;
                            messageRecipient.Status = MessageRecipientStatus.Bounced;
                            continue;
                        }
                        /*Στέλνουμε μόνο όσα είναι σε status Pending:*/
                        if (messageRecipient.Status != MessageRecipientStatus.Pending)
                        {
                            message.SkipCounter++;
                            continue;
                        }


                        //TODO:
                        //Ελεγχος για τα QUOTAS του Πελάτη



                        if (collector.UseCredits && collector.CreditType.HasValue && collector.CreditType.Value == CreditType.EmailType)
                        {
                            #region Πραγματοποιούμε την ΧΡΕΩΣΗ για αυτό το email που πρόκειται να στείλουμε:
                            bool charged = false;
                            if (messageRecipient.CollectorPayment.HasValue)
                            {
                                charged = SystemManager.ChargePaymentForEmail(messageRecipient.CollectorPayment.Value, collector.CollectorId, message.MessageId, recipient.RecipientId);
                            }
                            else
                            {
                                charged = true;
                            }
                            if (charged == false)
                            {
                                DebugFormat("Sending email to {0} (messageId = {1}, recipient = {2})-> NoCredit", recipient.Email, message.MessageId, recipient.RecipientId);
                                message.FailedCounter++;
                                messageRecipient.ErrorCount++;
                                messageRecipient.Status = MessageRecipientStatus.NoCredit;
                                continue;
                            }
                            if (messageRecipient.CollectorPayment.HasValue)
                            {
                                messageRecipient.IsCharged = true;
                            }
                            #endregion
                        }


                        /*
                         * ΑΠΟ ΕΔΩ ΚΑΙ ΚΑΤΩ ΕΧΟΥΜΕ ΧΡΕΩΣEI ΓΙΑ ΤΗΝ ΑΠΟΣΤΟΛΗ ΤΟΥ EMAIL.
                         * ΓΙΑ ΑΥΤΟ ΕΑΝ ΣΥΜΒΕΙ ΚΑΤΙ ΦΡΟΝΤΙΖΟΥΜΕ ΝΑ ΞΕΧΡΕΩΣΟΥΜΕ, ΠΡΩΤΑ:
                         */
                        try
                        {
                            /*Προετοιμάζουμε το body του μηνύματος, αντικαθιστώντας τυχόν placeholders:*/
                            var subject = message.Subject;
                            var body    = message.Body;
                            body = body.Replace("[SurveyLink]", Utility.GetSurveyRuntimeURL(survey, collector, recipient));
                            body = body.Replace("[RemoveLink]", Utility.GetRemoveRecipientURL(survey, collector, message, recipient));
                            var         displayName = string.Format("{0} via {1}", message.Sender, fromDisplayName.ParameterValue);
                            MailAddress from        = new MailAddress(message.Sender, displayName, Encoding.UTF8);
                            MailAddress to          = new MailAddress(recipient.Email);
                            MailAddress replyTo     = new MailAddress(message.Sender);

                            bool emailed = provider.SendEmail(from, to, replyTo, subject, Encoding.UTF8, body, Encoding.UTF8, false);
                            if (emailed == false)
                            {
                                DebugFormat("Sending email to {0} (messageId = {1}, recipient = {2})-> FAILED", recipient.Email, message.MessageId, recipient.RecipientId);
                                message.FailedCounter++;
                                messageRecipient.ErrorCount++;
                                messageRecipient.Status = MessageRecipientStatus.Failed;
                                messageRecipient.Error  = "provider.SendEmail() returned false!";
                                continue;
                            }


                            DebugFormat("Sending email to {0} (messageId = {1}, recipient = {2})-> SUCCESS", recipient.Email, message.MessageId, recipient.RecipientId);
                            message.SentCounter++;
                            messageRecipient.ErrorCount = 0;
                            messageRecipient.Status     = MessageRecipientStatus.Sent;
                        }
                        catch
                        {
                            if (collector.CreditType.HasValue && collector.CreditType.Value == CreditType.EmailType)
                            {
                                #region Ξεχρεώνουμε
                                if (messageRecipient.CollectorPayment.HasValue && messageRecipient.IsCharged)
                                {
                                    bool uncharged = SystemManager.UnchargePaymentForEmail(messageRecipient.CollectorPayment.Value, collector.CollectorId, message.MessageId, recipient.RecipientId);
                                    if (uncharged)
                                    {
                                        messageRecipient.IsCharged = false;
                                    }
                                }
                                #endregion
                            }
                            throw;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Error(string.Format("Sending email to {0} (messageId = {1}, recipient = {2})-> Exception", recipient.Email, message.MessageId, recipient.RecipientId), ex);
                        message.FailedCounter++;
                        if (messageRecipient != null)
                        {
                            messageRecipient.ErrorCount++;
                            messageRecipient.Status = MessageRecipientStatus.Failed;
                            messageRecipient.Error  = Utility.UnWindExceptionContent(ex);
                        }
                    }
                    finally
                    {
                        if (messageRecipient != null)
                        {
                            try
                            {
                                messageRecipient = SurveyManager.UpdateMessageRecipient(messageRecipient);

                                if (messageRecipient.Status == MessageRecipientStatus.Sent)
                                {
                                    /*το email, στάλθηκε, πρέπει να ενημερώσουμε και το Recipient:*/
                                    if (recipient.IsSentEmail == false)
                                    {
                                        recipient.IsSentEmail = true;
                                        var updatedRecipient = SurveyManager.UpdateRecipientIntl(recipient);
                                    }
                                }
                            }
                            catch (Exception innerEx)
                            {
                                this.Error(string.Format("TheMailler::SendMails():finally"), innerEx);
                            }
                        }
                    }
                    #endregion
                }

                recipients = SurveyManager.GetRecipientsForMessage(message, pageIndex++, pageSize, ref totalRows);
            }

            provider.Dispose();
        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                bool        hadCollectors = this.SelectedSurvey.HasCollectors;
                VLCollector collector     = null;

                //Αναλόγως του τύπου Collector που επιλέχτηκε, δημιοργούμε έναν:
                if (this.rdbtnWebLink.Checked)
                {
                    if (this.UseCredits)
                    {
                        if (this.ShowCreditTypeSelector)
                        {
                            //Ο πελάτης επιλέγει το CreditType:
                            CreditType _resourceType = (CreditType)Enum.Parse(typeof(CreditType), this.frmCreditType.SelectedValue);
                            collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.WebLink, this.collectorName.Text, creditType: _resourceType);
                        }
                        else
                        {
                            //Επιλέουμε εμείς αυτόματα το CreditType:
                            CreditType _resourceType = CreditType.ClickType;
                            collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.WebLink, this.collectorName.Text, creditType: _resourceType);
                        }
                    }
                    else
                    {
                        collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.WebLink, this.collectorName.Text);
                    }
                }
                else if (this.rdbtnEmailList.Checked)
                {
                    if (this.UseCredits)
                    {
                        if (this.ShowCreditTypeSelector)
                        {
                            //Ο πελάτης επιλέγει το CreditType:
                            CreditType _resourceType = (CreditType)Enum.Parse(typeof(CreditType), this.frmCreditType.SelectedValue);
                            collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.Email, this.collectorName.Text, creditType: _resourceType);
                        }
                        else
                        {
                            //Επιλέουμε εμείς αυτόματα το CreditType:
                            CreditType _resourceType = CreditType.EmailType;
                            collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.Email, this.collectorName.Text, creditType: _resourceType);
                        }
                    }
                    else
                    {
                        collector = SurveyManager.CreateCollector(this.Surveyid, CollectorType.Email, this.collectorName.Text);
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }


                //Μετά την δημιουργία πάμε κατευθείαν στην αντίστοιχη edit οθόνη:
                if (collector.CollectorType == CollectorType.WebLink)
                {
                    Response.Redirect(string.Format("details-weblink.aspx?surveyid={0}&collectorId={1}&textslanguage={2}", this.Surveyid, collector.CollectorId, collector.TextsLanguage), false);
                    this.Context.ApplicationInstance.CompleteRequest();
                }
                else if (collector.CollectorType == CollectorType.Email)
                {
                    Response.Redirect(string.Format("details-email.aspx?surveyid={0}&collectorId={1}&textslanguage={2}", this.Surveyid, collector.CollectorId, collector.TextsLanguage), false);
                    this.Context.ApplicationInstance.CompleteRequest();
                }
                else if (collector.CollectorType == CollectorType.Website)
                {
                    Response.Redirect(string.Format("details-website.aspx?surveyid={0}&collectorId={1}&textslanguage={2}", this.Surveyid, collector.CollectorId, collector.TextsLanguage), false);
                    this.Context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            catch (ThreadAbortException)
            {
                //
            }
            catch (Exception ex)
            {
                this.ErrorMessage = ex.Message;
            }
        }