public OperationResult SendUnsentEmailInDatabase()
        {
            IBusinessService emailSentBusinessService = new EmailSentBusinessService();
            SelectParam      selectParam = new SelectParam();

            selectParam.Where = null;
            selectParam.Where = "IsSent = 0";
            OperationResult result = emailSentBusinessService.FindAll(selectParam, null, null);

            List <object> emailSentList = (List <object>)result.Data;

            foreach (object emailSent in emailSentList)
            {
                OperationResult resultSendEmail = SendUnsentEmailItem(emailSent);
                emailSent.GetType().GetProperty("SentDate").SetValue(emailSent, DateTime.Now, null);
                if (resultSendEmail.Result)
                {
                    emailSent.GetType().GetProperty("IsSent").SetValue(emailSent, 1, null);
                }
                else
                {
                    string errorMessage = "";

                    Exception e = (Exception)resultSendEmail.Data;
                    errorMessage = e.Message + "\n\n";
                    if (e.InnerException != null)
                    {
                        errorMessage += e.InnerException.Message;
                    }
                    emailSent.GetType().GetProperty("IsSent").SetValue(emailSent, 0, null);

                    emailSent.GetType().GetProperty("FailReason").SetValue(emailSent, errorMessage, null);
                }

                try
                {
                    emailSentBusinessService.Update(emailSent);
                }
                catch (Exception err)
                {
                    result.Data = err;
                }
            }


            return(result);
        }
        protected virtual OperationResult SaveToDatabase(SenderEmailAccountForm senderEmailAccount, EmailTemplateForm emailTemplate, SendEmailForm sendMailForm)
        {
            object obj = Factory.Create <object>("EmailSent", ClassType.clsTypeDataModel);

            obj.GetType().GetProperty("SenderName").SetValue(obj, senderEmailAccount.SenderName, null);
            obj.GetType().GetProperty("EmailFrom").SetValue(obj, senderEmailAccount.EmailAccount, null);
            obj.GetType().GetProperty("EmailFromAlias").SetValue(obj, senderEmailAccount.SenderEmail, null);
            obj.GetType().GetProperty("EmailSubject").SetValue(obj, emailTemplate.EmailSubject, null);
            obj.GetType().GetProperty("EmailContent").SetValue(obj, emailTemplate.EmailContent, null);
            obj.GetType().GetProperty("SentDate").SetValue(obj, DateTime.Now, null);
            obj.GetType().GetProperty("EmailTos").SetValue(obj, sendMailForm.To, null);
            obj.GetType().GetProperty("EmailCcs").SetValue(obj, sendMailForm.Cc, null);
            obj.GetType().GetProperty("UserId").SetValue(obj, senderEmailAccount.UserId, null);
            obj.GetType().GetProperty("IsSent").SetValue(obj, 0, null);
            obj.GetType().GetProperty("EmailAccountId").SetValue(obj, senderEmailAccount.IdSender, null);


            EmailSentBusinessService businessService = new EmailSentBusinessService();
            OperationResult          result          = businessService.Add(obj);
            object emailSent = result.Data;

            EmailAttachmentBusinessService bs = new EmailAttachmentBusinessService();

            if (sendMailForm.Attachments != null)
            {
                foreach (EmailAttachment attachment in sendMailForm.Attachments)
                {
                    attachment.EmailSentId = Convert.ToInt64(emailSent.GetType().GetProperty("IdEmailSent").GetValue(emailSent, null));
                    bs.Add(attachment);
                }
            }

            // emailSent.EmailContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(emailSent.EmailContent));
            result.Data = emailSent;
            return(result);
        }