Example #1
0
        public async Task SendNewCustomer(Customer customer, User user)
        {
            var mailMessage = await _emailBuilder.Build(EmailTemplateName.NewCustomer, new Dictionary <string, string>());

            _emailDistributionService.SetDistributionList(ref mailMessage);
            mailMessage.Subject = "New Customer";
            mailMessage.From    = new EmailAddress("*****@*****.**", "Kids Toy Hive");
            mailMessage.AddTo($"{customer.Email}", $"{customer.FirstName} {customer.LastName}");
            await _emailDeliveryService.Send(mailMessage);
        }
        public async Task SendAsync(SendNotificationAdto sendNotificationAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    NotificationType notificationType = await
                                                        _queryRepository.GetSingleAsync(n => n.Type == sendNotificationAdto.NotificationType);

                    if (notificationType != null)
                    {
                        IEnumerable <NotificationTypeChannel> notificationTypeNotificationTypeChannels = notificationType.NotificationTypeChannels;

                        foreach (NotificationTypeChannel notificationTypeChannel in notificationTypeNotificationTypeChannels)
                        {
                            IChannelAudienceResolver channelAudienceResolver =
                                _channelAudienceResolverProvider.GetByType(notificationTypeChannel.ChannelType,
                                                                           sendNotificationAdto.NotificationType);

                            IEnumerable <string> emailAddresses = await channelAudienceResolver.GetAudienceAsync(sendNotificationAdto.IdentityId);

                            switch (notificationTypeChannel.ChannelType)
                            {
                            case ChannelType.Email:
                                foreach (string emailAddress in emailAddresses)
                                {
                                    if (!(notificationTypeChannel.NotificationChannelTemplate is EmailChannelTemplate emailChannelTemplate))
                                    {
                                        throw new NullReferenceException("No template defined for channel");
                                    }

                                    EmailAdto emailAdto = _emailBuilder.Build(new BuildEmailAdto
                                    {
                                        EmailTemplate = emailChannelTemplate.Template,
                                        Subject       = emailChannelTemplate.Subject,
                                        PropertyBag   = sendNotificationAdto.PropertyBag
                                    });

                                    await Message.SendAsync(SendEmailNotificationMessage.Create("*****@*****.**", emailAddress, emailAdto.Subject, emailAdto.HtmlBody));
                                }

                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                    }

                    transaction.Commit();
                }
                catch (BusinessApplicationException e)
                {
                    _logger.LogDebug(e.Message);
                }
            }
        }