Exemplo n.º 1
0
        static SendMessageResult GetEmailOptions(MessageTemplate messageTemplate, MessageInfo message, string emailCategory, IEnumerable <string> emails, bool asynchronously, out SendEmailOptions sendEmailOptions)
        {
            sendEmailOptions = null;
            if (messageTemplate == null)
            {
                throw new ArgumentNullException(nameof(MessageTemplate));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(MessageInfo));
            }
            if (string.IsNullOrWhiteSpace(messageTemplate.Title))
            {
                throw new Exception("The message template title is null or empty");
            }
            if (string.IsNullOrWhiteSpace(messageTemplate.Content))
            {
                throw new Exception("The message template content is null or empty");
            }
            if (emails.IsNullOrEmpty())
            {
                throw new Exception("The emails is null or empty");
            }
            var parameterDict = message.Parameters?.ObjectToStringDcitionary();
            Dictionary <string, string> templateParameters = GetTemplateParameters(parameterDict);

            //title
            var titleResolveResult = ResolveTemplate(messageTemplate.Title, templateParameters);

            if (!titleResolveResult.Item1 || string.IsNullOrWhiteSpace(titleResolveResult.Item3))
            {
                if (!string.IsNullOrWhiteSpace(titleResolveResult.Item2))
                {
                    return(SendMessageResult.NoParameter($"Not set '{titleResolveResult.Item2}' value in the email title template"));
                }
                return(SendMessageResult.MessageIsNullOrEmpty($"The email title is null or empty"));
            }

            //content
            var contentResolveResult = ResolveTemplate(messageTemplate.Content, templateParameters);

            if (!contentResolveResult.Item1 || string.IsNullOrWhiteSpace(contentResolveResult.Item3))
            {
                if (!string.IsNullOrWhiteSpace(contentResolveResult.Item2))
                {
                    return(SendMessageResult.NoParameter($"Not set '{contentResolveResult.Item2}' value in the email body template"));
                }
                return(SendMessageResult.MessageIsNullOrEmpty($"The email body is null or empty"));
            }

            //Send email
            sendEmailOptions = new SendEmailOptions()
            {
                Category       = emailCategory,
                Content        = contentResolveResult.Item3,
                Subject        = titleResolveResult.Item3,
                Asynchronously = asynchronously,
                Emails         = emails
            };
            sendEmailOptions.AddWorkId(message.WorkId);
            sendEmailOptions.AddTemplateMessageId(message.Id);
            return(SendMessageResult.SendSuccess());
        }