Esempio n. 1
0
        /// <summary>
        /// Processes the exception.
        /// </summary>
        /// <param name="queueEntity">The queue entity.</param>
        /// <param name="ex">The ex.</param>
        private void ProcessException(List <MailMessage> outputMessages, List <OutgoingMessageQueueEntity> queueEntity, Exception ex)
        {
            for (int index = 0; index < outputMessages.Count; index++)
            {
                MailMessage message = outputMessages[index];
                OutgoingMessageQueueEntity entity = queueEntity[index];

                if (message.SentDate != DateTime.MinValue)
                {
                    entity.Error = string.Empty;
                    entity.DeliveryAttempts++;
                    entity.IsDelivered = true;
                }
                else
                {
                    entity.Error = ex.Message;
                    entity.DeliveryAttempts++;
                }

                BusinessManager.Update(entity);
            }

            //foreach (OutgoingMessageQueueEntity entity in queueEntity)
            //{
            //    entity.Error = ex.Message;
            //    entity.DeliveryAttempts++;

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

            //    BusinessManager.Update(entity);
            //}
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the message to outgoing queue.
        /// </summary>
        /// <param name="referenceFieldName">Name of the reference field.</param>
        /// <param name="primaryKeyId">The primary key id.</param>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        protected PrimaryKeyId AddMessageToOutgoingQueue(string metaClassName, string referenceFieldName, PrimaryKeyId primaryKeyId, string source)
        {
            OutgoingMessageQueueEntity newElement = BusinessManager.InitializeEntity <OutgoingMessageQueueEntity>(OutgoingMessageQueueEntity.ClassName);

            newElement[referenceFieldName] = primaryKeyId;
            newElement.Source = source;

            return(BusinessManager.Create(newElement));
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the entity object.
        /// </summary>
        /// <param name="metaClassName">Name of the meta class.</param>
        /// <param name="primaryKeyId">The primary key id.</param>
        /// <returns></returns>
        protected override EntityObject CreateEntityObject(string metaClassName, PrimaryKeyId?primaryKeyId)
        {
            if (metaClassName == OutgoingMessageQueueEntity.ClassName)
            {
                OutgoingMessageQueueEntity retVal = new OutgoingMessageQueueEntity();
                retVal.PrimaryKeyId = primaryKeyId;
                return(retVal);
            }

            return(base.CreateEntityObject(metaClassName, primaryKeyId));
        }
Esempio n. 4
0
        /// <summary>
        /// Processes the send result.
        /// </summary>
        /// <param name="outputMessages">The output messages.</param>
        /// <param name="queueEntity">The queue entity.</param>
        private void ProcessSendResult(List <MailMessage> outputMessages, List <OutgoingMessageQueueEntity> queueEntity)
        {
            for (int index = 0; index < outputMessages.Count; index++)
            {
                MailMessage message = outputMessages[index];
                OutgoingMessageQueueEntity entity = queueEntity[index];

                if (message.SentDate != DateTime.MinValue)
                {
                    // Mark Message As Delivered
                    entity.Error = string.Empty;
                    entity.DeliveryAttempts++;
                    entity.IsDelivered = true;
                }
                else
                {
                    entity.Error = message.ErrorMessage;
                    entity.DeliveryAttempts++;
                }

                BusinessManager.Update(entity);
            }
        }
Esempio n. 5
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();
            }

            //
        }