public string SendProtocolMail(string mailDTO, string protocolDTO, string taskDTO)
        {
            try
            {
                var mail     = mailDTO.Deserialize <MailDTO>();
                var protocol = protocolDTO.Deserialize <ProtocolDTO>();
                if (!protocol.HasAnyDocument() && protocol.HasId())
                {
                    var domain = FacadeFactory.Instance.ProtocolFacade.GetById(protocol.UniqueId.Value);
                    protocol.CopyFrom(domain);
                }
                mail.CopyFrom(protocol);

                if (mail.Sender != null && !((MailboxDTO)mail.Mailbox).IsPOL())
                {
                    if (!RegexHelper.IsValidEmail(mail.Sender.EmailAddress))
                    {
                        mail.Sender.Code         = mail.Sender.EmailAddress;
                        mail.Sender.EmailAddress = SearchEmailAddressFromCode(mail.Sender.Code);
                    }
                }

                if (mail.HasRecipients())
                {
                    foreach (var item in mail.Recipients)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                if (mail.HasRecipientsCc())
                {
                    foreach (var item in mail.RecipientsCc)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                if (mail.HasRecipientsBcc())
                {
                    foreach (var item in mail.RecipientsBcc)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                mail = MailService.Send(mail, protocol);

                mail = PairToProtocol(mail, protocol);

                var task = taskDTO.Deserialize <TaskDTO>();
                task.AddMail(mail);
                task = TaskService.Update(task);

                return(mail.SerializeAsResponse());
            }
            catch (Exception ex)
            {
                LogError(ex);
                var response = new APIResponse <MailDTO>(ex);
                return(response.Serialize());
            }
            finally
            {
                NHibernateSessionManager.Instance.CloseTransactionAndSessions();
            }
        }
        public string SendMail(string mailDTO, string taskDTO)
        {
            try
            {
                var mail = mailDTO.Deserialize <MailDTO>();

                if (mail.Sender != null && !((MailboxDTO)mail.Mailbox).IsPOL())
                {
                    if (!RegexHelper.IsValidEmail(mail.Sender.EmailAddress))
                    {
                        mail.Sender.Code         = mail.Sender.EmailAddress;
                        mail.Sender.EmailAddress = SearchEmailAddressFromCode(mail.Sender.Code);
                    }
                }

                if (mail.HasRecipients())
                {
                    foreach (var item in mail.Recipients)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                if (mail.HasRecipientsCc())
                {
                    foreach (var item in mail.RecipientsCc)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                if (mail.HasRecipientsBcc())
                {
                    foreach (var item in mail.RecipientsBcc)
                    {
                        if (!RegexHelper.IsValidEmail(item.EmailAddress))
                        {
                            item.Code         = item.EmailAddress;
                            item.EmailAddress = SearchEmailAddressFromCode(item.Code);
                        }
                    }
                }

                var result = MailService.Send(mail);

                var task = taskDTO.Deserialize <TaskDTO>();
                task.AddMail(mail);
                task = TaskService.Update(task);

                return(result.SerializeAsResponse());
            }
            catch (Exception ex)
            {
                LogError(ex);
                var response = new APIResponse <MailDTO>(ex);
                return(response.Serialize());
            }
            finally
            {
                NHibernateSessionManager.Instance.CloseTransactionAndSessions();
            }
        }