Esempio n. 1
0
        /// <summary>
        /// E-mail a message.
        /// </summary>
        /// <typeparam name="M">The type of the model in the message.</typeparam>
        /// <param name="channelMessage">The message to send to the channel.</param>
        public async Task SendMessageAsync <M>(IChannelMessage <M, T> channelMessage)
        {
            if (channelMessage == null)
            {
                throw new ArgumentNullException(nameof(channelMessage));
            }

            var destinationIdentities = await GetDestinationIdentitiesAsync(channelMessage.Destination, channelMessage.Topic);

            if (!destinationIdentities.Any())
            {
                return;
            }

            bool useSingleMessageForMultipleRecepients = UseSingleMessageForMultipleRecepients(channelMessage.Destination, channelMessage.Topic);

            var emailDestinationAddressesCollection = GetEmailDestinationAddressesCollection(destinationIdentities, useSingleMessageForMultipleRecepients);

            var senderIdentity = await GetSenderIdentityAsync(channelMessage.Source, channelMessage.Topic);

            var senderAddress = GetMailAddress(senderIdentity);

            var destinationIdentitiesByEmail = destinationIdentities.ToReadOnlyMultiDictionary(i => i.Email);

            foreach (var emailDestinationAddresses in emailDestinationAddressesCollection)
            {
                using (var bodyWriter = new System.IO.StringWriter())
                {
                    var messageDestinationIdentities = from address in emailDestinationAddresses
                                                       where destinationIdentitiesByEmail.ContainsKey(address.Address)
                                                       from identity in destinationIdentitiesByEmail[address.Address]
                                                       select identity;

                    renderProvider.Render(
                        GetFullTemplateKey(channelMessage.TemplateKey),
                        bodyWriter,
                        channelMessage.Model,
                        GetDynamicProperties(channelMessage, messageDestinationIdentities));

                    string messageBody = bodyWriter.ToString();

                    string messageID = GetMessageID(channelMessage, messageDestinationIdentities);

                    await SendEmailMessageAsync(
                        channelMessage.Subject,
                        senderAddress,
                        emailDestinationAddresses,
                        messageBody,
                        messageID);
                }
            }
        }
        /// <summary>
        /// Render a template to string.
        /// </summary>
        /// <param name="renderProvider">The render provider implementation.</param>
        /// <param name="templateKey">The key of the template.</param>
        /// <param name="dynamicProperties">The dynamic properties.</param>
        public static string RenderToString(
            this IRenderProvider renderProvider,
            string templateKey,
            IDictionary <string, object> dynamicProperties)
        {
            if (renderProvider == null)
            {
                throw new ArgumentNullException(nameof(renderProvider));
            }

            using (var writer = new System.IO.StringWriter())
            {
                renderProvider.Render(templateKey, writer, dynamicProperties);

                return(writer.ToString());
            }
        }