/// <summary>
        /// Creates a <see cref="INotificationMessage"/> and saves it to the database
        /// </summary>
        /// <param name="methodKey">The <see cref="INotificationMethod"/> key</param>
        /// <param name="name">The name of the message (primarily used in the back office UI)</param>
        /// <param name="description">The name of the message (primarily used in the back office UI)</param>
        /// <param name="fromAddress">The senders or "from" address</param>
        /// <param name="recipients">A collection of recipient address</param>
        /// <param name="bodyText">The body text of the message</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        /// <returns>Attempt{INotificationMessage}</returns>
        public Attempt<INotificationMessage> CreateNotificationMethodWithKey(Guid methodKey, string name, string description, string fromAddress,
            IEnumerable<string> recipients, string bodyText, bool raiseEvents = true)
        {
            var recipientArray = recipients as string[] ?? recipients.ToArray();

            Mandate.ParameterCondition(methodKey != Guid.Empty, "methodKey");
            Mandate.ParameterNotNullOrEmpty(name, "name");
            Mandate.ParameterNotNullOrEmpty(fromAddress, "fromAddress");            

            var message = new NotificationMessage(methodKey, name, fromAddress)
            {
                Description = description,
                BodyText = bodyText,
                Recipients = string.Join(",", recipientArray)
            };

            if(raiseEvents)
            if (Creating.IsRaisedEventCancelled(new Events.NewEventArgs<INotificationMessage>(message), this))
            {
                message.WasCancelled = true;
                return Attempt<INotificationMessage>.Fail(message);
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateNotificationMessageRepository(uow))
                {
                    repository.AddOrUpdate(message);
                    uow.Commit();
                }
            }

            if(raiseEvents) Created.RaiseEvent(new Events.NewEventArgs<INotificationMessage>(message), this);

            return Attempt<INotificationMessage>.Succeed(message);
        }
        public void Can_Save_A_NotificationMessage()
        {
            //// Arrange
            var resource = _provider.ListResourcesOffered().FirstOrDefault();
            Assert.NotNull(resource, "Smtp Provider returned null for GatewayResource");
            var method = _provider.CreateNotificationMethod(resource, resource.Name, "SMTP Relayed Email");
            Assert.NotNull(method, "method was null");

            //// Act
            var message = new NotificationMessage(method.NotificationMethod.Key, "Test email",
                "*****@*****.**")
            {
                Recipients = "*****@*****.**",
                BodyText = "Successful test?"
            };

            method.SaveNotificationMessage(message);

            //// Assert
            Assert.IsTrue(message.HasIdentity);
        }
        public void Can_Send_A_Test_Email()
        {
            // check configuration to see if we want to do this
            if (!bool.Parse(ConfigurationManager.AppSettings["sendTestEmail"])) Assert.Ignore("Skipping test");

            var recipients = "*****@*****.**";

            //// Arrange
            var settings = _provider.ExtendedData.GetSmtpProviderSettings();
            settings.Host = "127.0.0.1";
            _provider.ExtendedData.SaveSmtpProviderSettings(settings);

            var resource = _provider.ListResourcesOffered().FirstOrDefault();
            Assert.NotNull(resource, "Smtp Provider returned null for GatewayResource");

            var method = _provider.CreateNotificationMethod(resource, resource.Name, "Test email method");

            //// Act
            var message = new NotificationMessage(method.NotificationMethod.Key, "Test email", "*****@*****.**")
            {
                Recipients = "[email protected],[email protected]",
                BodyText = "Successful test?"
            };

            method.Send(message);

            //Thread.Sleep(2000);
        }