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(); }