/// <summary>
        /// Invokes this instance.
        /// </summary>
        public override void Invoke()
        {
            // Remove Expired
            RemoveExpiredMessages();

            //int maxDeliveryAttempts = 100; // TODO: Read From Config

            EntityObject[] elements = BusinessManager.List(OutgoingMessageQueueEntity.ClassName,
                                                           OutgoingMessageUtil.CreateIbnClientMessageDeliveryProviderFilters(),
                                                           new SortingElement[]
            {
                SortingElement.Ascending(OutgoingMessageQueueEntity.FieldCreated)
            });

            foreach (OutgoingMessageQueueEntity element in elements)
            {
                try
                {
                    // Load Ibn Message
                    IbnClientMessageEntity message = (IbnClientMessageEntity)BusinessManager.Load(IbnClientMessageEntity.ClassName, element.IbnClientMessageId.Value);

                    // Send
                    int toOriginalId   = DBUser.GetOriginalId(message.ToId);
                    int fromOriginalId = DBUser.GetOriginalId(message.FromId);

                    IMHelper.SendMessage(toOriginalId, fromOriginalId, message.HtmlBody);

                    element.Error = string.Empty;
                    element.DeliveryAttempts++;
                    element.IsDelivered = true;
                }
                catch (Exception ex)
                {
                    element.Error = ex.Message;
                    element.DeliveryAttempts++;

                    // TODO: Save Complete Error Stack || Complete Delivery Log
                }

                BusinessManager.Update(element);
            }

            //
        }
Esempio n. 2
0
        public override void Invoke()
        {
            // TODO: Remove Expired
            RemoveExpiredMessages();

            //int maxDeliveryAttempts = 100; // TODO: Read From Config
            int maxMessagesPerConnection = 10;

            // Prepare temporary collection
            List <MailMessage> outputMessages             = new List <MailMessage>();
            List <OutgoingMessageQueueEntity> queueEntity = new List <OutgoingMessageQueueEntity>();

            // Load Outgoing Message Queue Entity
            EntityObject[] elements = BusinessManager.List(OutgoingMessageQueueEntity.ClassName,
                                                           OutgoingMessageUtil.CreateEmailDeliveryProviderFilters(),
                                                           new SortingElement[]
            {
                SortingElement.Ascending(OutgoingMessageQueueEntity.FieldSource),
                SortingElement.Ascending(OutgoingMessageQueueEntity.FieldCreated)
            });


            // Run Message Delivery Process
            for (int elementIndex = 0; elementIndex < elements.Length; elementIndex++)
            {
                // Read Element From Outgoing Message Queue
                OutgoingMessageQueueEntity element = (OutgoingMessageQueueEntity)elements[elementIndex];

                // Load Email Message
                EmailEntity emailEntity = (EmailEntity)BusinessManager.Load(EmailEntity.ClassName, element.EmailId.Value);

                // Create Output Mail Message
                MailMessage outputMessage = CopyEmailEntityToMailMessage(emailEntity);

                // Add Output message to Output queue
                outputMessages.Add(outputMessage);
                queueEntity.Add(element);

                string currentSource     = element.Source;
                string nextElementSource = (elementIndex < (elements.Length - 1)) ?
                                           ((OutgoingMessageQueueEntity)elements[elementIndex + 1]).Source :
                                           string.Empty;

                if (outputMessages.Count < maxMessagesPerConnection &&
                    currentSource == nextElementSource)
                {
                    continue;
                }

                // Send Output queue
                try
                {
                    // OZ [2010-03-03]: Check that SmtpServer is configured. Write To Outgoing log
                    if (SmtpBox.TotalCount() == 0)
                    {
                        throw new SmtpNotConfiguredException();
                    }

                    // Initialize Smtp Client
                    SmtpClient smtpClient = Mediachase.IBN.Business.EMail.SmtpClientUtility.CreateSmtpClient(element.Source);

                    // Send
                    smtpClient.Send(outputMessages.ToArray());

                    // Process result
                    ProcessSendResult(outputMessages, queueEntity);
                }
                catch (Exception ex)
                {
                    ProcessException(outputMessages, queueEntity, ex);
                }

                // Clear Output queue
                outputMessages.Clear();
                queueEntity.Clear();
            }

            //
        }