public void Handle(EmailReceivedMessage emailMessage)
        {
            Data.EmailReceivedMessage = emailMessage;

            var requestersDtoToCreate = new List <RequesterDTO>();

            var fromAddress = new MailAddressLite {
                Address = emailMessage.Mail.FromAddress, DisplayName = emailMessage.Mail.FromDisplayName
            };

            ProcessAddress(fromAddress, requestersDtoToCreate);

            if (emailMessage.Mail.CC != null)
            {
                foreach (var ccAddress in emailMessage.Mail.CC)
                {
                    ProcessAddress(ccAddress, requestersDtoToCreate);
                }
            }

            if (requestersDtoToCreate.Empty())
            {
                var fromRequester = GetRequesterByAddress(fromAddress.Address);
                InvokeRules(fromRequester.Id);
            }
            else
            {
                SendLocal(new CreateRequestersForMessageCommandInternal {
                    RequestersDto = requestersDtoToCreate.ToArray(), OuterSagaId = Data.Id
                });
            }
        }
        public bool ReadNextEmailMessage(string[] uids)
        {
            while (uids != null && _index < uids.Length)
            {
                var uid = uids[_index++];

                // 1. Download email message from the server.
                var mailBeeMailMessage = DoDownloadMailMessage(uid);

                if (mailBeeMailMessage == null)
                {
                    continue;
                }

                // 2. Mark downloaded message as read, do not download it the next time.
                var messageUid = MarkAsRead(uid);

                // Copy MailBee message to System.Net.Mail message.
                System.Net.Mail.MailMessage mailMessage;
                try
                {
                    mailMessage = ConvertToNetMailMessage(mailBeeMailMessage);
                }
                catch (Exception e)
                {
                    //Just skip this message
                    _log.Error(String.Format("Error parsing message."), e);
                    return(true);
                }

                // If incoming message has incorrect date MailBee may throw exception.
                var sentDate = SqlDateTime.MinValue.Value;
                try
                {
                    if (mailBeeMailMessage.Date != DateTime.MinValue)
                    {
                        sentDate = mailBeeMailMessage.Date;
                    }
                }
                catch
                {
                }

                var emailReceivedMessage = new EmailReceivedMessage {
                    Mail = ConvertToDto(mailMessage, messageUid, sentDate)
                };
                _localBus.SendLocal(emailReceivedMessage);

                return(true);
            }

            _client.Disconnect();

            return(false);
        }
        private static UserLite GetUserFrom(EmailReceivedMessage emailMessage)
        {
            var people = ObjectFactory.GetInstance <UserRepository>().GetByEmail(emailMessage.Mail.FromAddress).ToArray();

            var requesters = people.Where(x => x.UserType == UserType.Requester).ToArray();

            if (!requesters.Empty())
            {
                var requesterFrom = requesters.FirstOrDefault(x => !x.IsDeletedRequester);
                return(requesterFrom ?? requesters.FirstOrDefault());
            }


            return(people.Where(x => x.UserType == UserType.User).FirstOrDefault(x => !x.IsDeletedOrInactiveUser));
        }
        public void Handle(EmailReceivedMessage emailMessage)
        {
            Data.EmailReceivedMessage = emailMessage;
            var userFrom = GetUserFrom(emailMessage);

            if (userFrom != null)
            {
                RestoreDeletedRequester(userFrom);
                InvokeRules(userFrom.Id);
            }
            else
            {
                var requesterDto = CreateRequesterDTO(emailMessage.Mail);
                SendLocal(new CreateRequesterForMessageCommandInternal {
                    RequesterDto = requesterDto, OuterSagaId = Data.Id
                });
            }
        }