Beispiel #1
0
        public string SendMailByTag(SendEmailForm sendEmailForm)
        {
            IEmailBusinessService emailBusinessService = new EmailBusinessService();
            OperationResult       result = emailBusinessService.SendEmailByTag(sendEmailForm);

            emailBusinessService.SendUnsentEmailInDatabase();

            return(Utility.Serializer.Json.Serialize(result));
        }
        public OperationResult SendEmailByTag(SendEmailForm sendEmailForm)
        {
            SenderEmailAccountForm senderEmailAccount = GetSenderEmailAccountByTag(sendEmailForm.Tag);
            EmailTemplateForm      emailTemplate      = GetEmailTemplateByTag(sendEmailForm.Tag);

            if (senderEmailAccount != null && emailTemplate != null)
            {
                emailTemplate.EmailContent = ReplaceContent(emailTemplate.EmailContent, sendEmailForm.KeyValues);
                emailTemplate.EmailSubject = ReplaceContent(emailTemplate.EmailSubject, sendEmailForm.KeyValues);

                return(SaveToDatabase(senderEmailAccount, emailTemplate, sendEmailForm));
            }

            return(new OperationResult(false, "No sender and email template defined for tag = '" + sendEmailForm.Tag + "'"));
        }
        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);
        }