Ejemplo n.º 1
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();
        }
        void HandleHeartBeat()
        {
            //Debug("Entering HandleHeartBeat()...");
            VLMessage message = null;

            try
            {
                #region Σταματάμε τον timer για να μην έχουμε περιττό pressing
                if (heartBeatTimer.Enabled)
                {
                    heartBeatTimer.Enabled = false;
                }
                #endregion

                /*
                 * TODO: send some kind of heartbeat to show we are alive
                 */



                /*
                 * Θέλουμε το επόμενο pending message για αποστολή:
                 */
                message = SurveyManager.GetNextPendingMessage(minuteOffset: 2);
                if (message == null)
                {
                    return;
                }



                /*
                 * Σε αυτο το σημείο προετοιμάζουμε το μήνυμα για μαζική αποστολή
                 * Εδω έχουμε το περιθώριο να κάνουμε διαφορες ενέργειες προετοιμασίας, να ελέγξουμε την πληρωμή, κ.α.
                 */
                bool _isPaymentValid = SurveyManager.ValidatePaymentForMessage(message, true);
                if (_isPaymentValid == false)
                {
                    SurveyManager.PromoteMessageToPreparingErrorStatus(message, "Payment validation failed!");
                    message = null;
                    return;
                }



                /*
                 * Επειτα τo κάνουμε promote σε Status Prepared:
                 */
                this.Info(string.Format("Preparing message with id={0} and subject '{1}'", message.MessageId, message.Subject));
                SurveyManager.PromoteMessageToPreparedStatus(message);
                message = null;
            }
            catch (Exception ex)
            {
                this.Error("TheController::HandleHeartBeat()", ex);
                if (message != null)
                {
                    SurveyManager.PromoteMessageToPreparingErrorStatus(message, ex.Message);
                    message = null;
                }
            }
            finally
            {
                #region ξεκινάμε τον timer
                if (UseRealHeartBeat)
                {
                    heartBeatTimer.Enabled = true;
                }
                #endregion
                //Debug("Leaving HandleHeartBeat()...");
            }
        }
Ejemplo n.º 3
0
        void HandleHeartBeat()
        {
            //DebugFormat("({0})  Entering HandleHeartBeat()...", this.Name);
            VLMessage message = null;

            try
            {
                #region Σταματάμε τον timer για να μην έχουμε περιττό pressing
                if (heartBeatTimer.Enabled)
                {
                    heartBeatTimer.Enabled = false;
                }
                #endregion

                /*
                 * TODO: send some kind of heartbeat to show we are alive
                 */



                /*
                 * Θέλουμε το επόμενο prepared message για  αποστολή
                 */
                message = SurveyManager.GetNextPreparedMessage();
                if (message == null)
                {
                    return;
                }

                #region fetch collector & survey:
                var collector = SurveyManager.GetCollectorById(message.Collector);
                if (collector == null)
                {
                    throw new VLException(SR.GetString(SR.There_is_no_item_with_id, "Collector", message.Collector));
                }
                if (collector.CollectorType != CollectorType.Email)
                {
                    throw new VLException(string.Format("Collector '{0}', has wrong CollectorType.", collector.Name));
                }
                var survey = SurveyManager.GetSurveyById(collector.Survey);
                if (survey == null)
                {
                    throw new VLException(SR.GetString(SR.There_is_no_item_with_id, "Survey", collector.Survey));
                }
                #endregion


                SendMails(survey, collector, message);


                /*Η αποστολή τελείωσε*/
                if (message.FailedCounter > 0)
                {
                    message = SurveyManager.PromoteMessageToExecutedWithErrorsStatus(message);
                }
                else
                {
                    message = SurveyManager.PromoteMessageToExecutedStatus(message);
                }

                if (collector.HasSentEmails == false && message.SentCounter > 0)
                {
                    collector.HasSentEmails = true;
                    collector = SurveyManager.UpdateCollector(collector);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    if (message != null)
                    {
                        if (message.Status != MessageStatus.Executed)
                        {
                            //Το exception έγινε πρίν τελειώσει η εκτέλεση της αποστολής των emails:
                            this.Error(string.Format("Exception while sending emails for messageid={0}", message.MessageId), ex);
                            SurveyManager.PromoteMessageToExecutingErrorStatus(message, ex);
                            message = null;
                        }
                        else
                        {
                            //To exception έγινε μετά την αποστολή των emails και την κλ΄σησ της μεθόδου PromoteMessageToExecutedStatus:
                            this.Error(string.Format("Exception after sending emails for messageid={0}. The delivery of the emails is completed!", message.MessageId), ex);
                            //SurveyManager.PromoteMessageToExecutingErrorStatus(message, ex);
                            message = null;
                        }
                    }
                    else
                    {
                        this.Error("TheMailler::HandleHeartBeat()", ex);
                    }
                }
                catch (Exception innerex)
                {
                    this.Error("TheMailler::HandleHeartBeat()", innerex);
                }
            }
            finally
            {
                #region ξεκινάμε τον timer
                if (UseRealHeartBeat)
                {
                    heartBeatTimer.Enabled = true;
                }
                #endregion
                //DebugFormat("({0})  Leaving HandleHeartBeat()...", this.Name);
            }
        }
Ejemplo n.º 4
0
        void createNewMessage()
        {
            VLMessage message = null;

            try
            {
                //Θα δημιουργήσουμε ένα καινούργιο message

                if (this.rdbtnNewAndUnsent.Checked)
                {
                    message = SurveyManager.CreateMessage(this.CollectorId);
                    message.DeliveryMethod = Valis.Core.DeliveryMethod.NewAndUnsent;
                }
                else if (this.rdbtnNotResponded.Checked)
                {
                    message = SurveyManager.CreateMessage(this.CollectorId);
                    message.DeliveryMethod = Valis.Core.DeliveryMethod.NotResponded;
                }
                else if (this.rdbtnAllResponded.Checked)
                {
                    message = SurveyManager.CreateMessage(this.CollectorId);
                    message.DeliveryMethod = Valis.Core.DeliveryMethod.AllResponded;
                }
                else if (this.rdbtnAll.Checked)
                {
                    message = SurveyManager.CreateMessage(this.CollectorId);
                    message.DeliveryMethod = Valis.Core.DeliveryMethod.All;;
                }
                else if (this.rdbtnCustom.Checked)
                {
                    if (string.IsNullOrWhiteSpace(this.txtKeyword.Text))
                    {
                        throw new VLException("Custom Criteria are invalid!");
                    }
                    if (string.IsNullOrWhiteSpace(ddlSearchField.SelectedValue))
                    {
                        throw new VLException("Custom Criteria are invalid!");
                    }
                    if (string.IsNullOrWhiteSpace(ddlCriteria.SelectedValue))
                    {
                        throw new VLException("Custom Criteria are invalid!");
                    }

                    message = SurveyManager.CreateMessage(this.CollectorId);
                    message.DeliveryMethod    = Valis.Core.DeliveryMethod.Custom;
                    message.CustomSearchField = (RecipientSearchField)Enum.Parse(typeof(RecipientSearchField), this.ddlSearchField.SelectedValue);
                    message.CustomOperator    = (ComparisonOperator)Enum.Parse(typeof(ComparisonOperator), this.ddlCriteria.SelectedValue);
                    message.CustomKeyword     = txtKeyword.Text;
                }

                message.IsDeliveryMethodOK = true;
                message = SurveyManager.UpdateMessage(message);

                Response.Redirect(string.Format("~/clay/mysurveys/collectors/editMessage_content.aspx?surveyId={0}&collectorId={1}&messageId={2}&textslanguage={3}", this.Surveyid, this.CollectorId, message.MessageId, this.TextsLanguage), false);
                this.Context.ApplicationInstance.CompleteRequest();
            }
            catch (ThreadAbortException)
            {
                //
            }
            catch (Exception ex)
            {
                this.ErrorMessage = ex.Message;
                if (message != null)
                {
                    SurveyManager.DeleteMessage(message);
                }
            }
        }