Beispiel #1
0
        /// <summary>
        /// Sends the user registered notification.
        /// </summary>
        /// <param name="userName">The user.</param>
        public static void SendUserRegisteredNotification(string userName)
        {
            if (userName == "")
            {
                throw new ArgumentNullException("userName");
            }

            var user = GetUser(userName);

            if (user.ProviderUserKey == null)
            {
                throw new ArgumentNullException("userName");
            }

            // TODO - create this via dependency injection at some point.
            IMailDeliveryService mailService = new SmtpMailDeliveryService();

            var          emailFormatType = HostSettingManager.Get(HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);
            var          emailFormatKey  = (emailFormatType == EmailFormatType.Text) ? "" : "HTML";
            const string subjectKey      = "UserRegisteredSubject";
            var          bodyKey         = string.Concat("UserRegistered", emailFormatKey);
            var          profile         = new WebProfile().GetProfile(user.UserName);

            var nc = new CultureNotificationContent().LoadContent(profile.PreferredLocale, subjectKey, bodyKey);

            var notificationUser = new NotificationUser
            {
                Id           = (Guid)user.ProviderUserKey,
                CreationDate = user.CreationDate,
                Email        = user.Email,
                UserName     = user.UserName,
                DisplayName  = profile.DisplayName,
                FirstName    = profile.FirstName,
                LastName     = profile.LastName,
                IsApproved   = user.IsApproved
            };

            var data = new Dictionary <string, object> {
                { "User", notificationUser }
            };

            var emailSubject = nc.CultureContents
                               .First(p => p.ContentKey == subjectKey)
                               .FormatContent();

            var bodyContent = nc.CultureContents
                              .First(p => p.ContentKey == bodyKey)
                              .TransformContent(data);

            var message = new MailMessage
            {
                Subject    = emailSubject,
                Body       = bodyContent,
                IsBodyHtml = true
            };

            mailService.Send(user.Email, message, null);
        }
Beispiel #2
0
        public SmtpMailDeliveryServiceTests()
        {
            _globalSettings = new GlobalSettings();
            _logger         = Substitute.For <ILogger <SmtpMailDeliveryService> >();

            _globalSettings.Mail.Smtp.Host    = "unittests.example.com";
            _globalSettings.Mail.ReplyToEmail = "*****@*****.**";

            _sut = new SmtpMailDeliveryService(_globalSettings, _logger);
        }
Beispiel #3
0
        /// <summary>
        /// Sends the forgot password email.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="token">The token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// user
        /// or
        /// user
        /// </exception>
        public static void SendForgotPasswordEmail(MembershipUser user, string token)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (user.ProviderUserKey == null)
            {
                throw new ArgumentNullException("user");
            }

            IMailDeliveryService mailService = new SmtpMailDeliveryService();

            var          emailFormatType = HostSettingManager.Get(HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);
            var          emailFormatKey  = (emailFormatType == EmailFormatType.Text) ? "" : "HTML";
            const string subjectKey      = "ForgotPasswordSubject";
            var          bodyKey         = string.Concat("ForgotPassword", emailFormatKey);
            var          profile         = new WebProfile().GetProfile(user.UserName);

            var nc = new CultureNotificationContent().LoadContent(profile.PreferredLocale, subjectKey, bodyKey);

            var notificationUser = new NotificationUser
            {
                Id           = (Guid)user.ProviderUserKey,
                CreationDate = user.CreationDate,
                Email        = user.Email,
                UserName     = user.UserName,
                DisplayName  = profile.DisplayName,
                FirstName    = profile.FirstName,
                LastName     = profile.LastName,
                IsApproved   = user.IsApproved
            };

            var data = new Dictionary <string, object>
            {
                { "Token", token }
            };

            var emailSubject = nc.CultureContents
                               .First(p => p.ContentKey == subjectKey)
                               .FormatContent();

            var bodyContent = nc.CultureContents
                              .First(p => p.ContentKey == bodyKey)
                              .TransformContent(data);

            var message = new MailMessage
            {
                Subject    = emailSubject,
                Body       = bodyContent,
                IsBodyHtml = true
            };

            mailService.Send(user.Email, message, null);
        }
        /// <summary>
        /// Sends an email to all users that are subscribed to a issue
        /// </summary>
        /// <param name="issueId">The issue id.</param>
        public static void SendIssueNotifications(int issueId)
        {
            if (issueId <= Globals.NEW_ID)
            {
                throw (new ArgumentOutOfRangeException("issueId"));
            }

            // TODO - create this via dependency injection at some point.
            IMailDeliveryService mailService = new SmtpMailDeliveryService();

            var issue            = DataProviderManager.Provider.GetIssueById(issueId);
            var issNotifications = DataProviderManager.Provider.GetIssueNotificationsByIssueId(issueId);
            var emailFormatType  = HostSettingManager.Get(HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);

            var data = new Dictionary <string, object> {
                { "Issue", issue }
            };

            var displayname = UserManager.GetUserDisplayName(Security.GetUserName());

            var          templateCache  = new List <CultureNotificationContent>();
            var          emailFormatKey = (emailFormatType == EmailFormatType.Text) ? "" : "HTML";
            const string subjectKey     = "IssueUpdatedSubject";
            var          bodyKey        = string.Concat("IssueUpdated", emailFormatKey);

            // get a list of distinct cultures
            var distinctCultures = (from c in issNotifications
                                    select c.NotificationCulture
                                    ).Distinct().ToList();

            // populate the template cache of the cultures needed
            foreach (var culture in from culture in distinctCultures let notificationContent = templateCache.FirstOrDefault(p => p.CultureString == culture) where notificationContent == null select culture)
            {
                templateCache.Add(new CultureNotificationContent().LoadContent(culture, subjectKey, bodyKey));
            }

            foreach (var notification in issNotifications)
            {
                try
                {
                    //send notifications to everyone except who changed it.
                    if (notification.NotificationUsername.ToLower() == Security.GetUserName().ToLower())
                    {
                        continue;
                    }

                    var user = UserManager.GetUser(notification.NotificationUsername);

                    // skip to the next user if this user is not approved
                    if (!user.IsApproved)
                    {
                        continue;
                    }
                    // skip to next user if this user doesn't have notifications enabled.
                    if (!new WebProfile().GetProfile(user.UserName).ReceiveEmailNotifications)
                    {
                        continue;
                    }

                    var nc = templateCache.First(p => p.CultureString == notification.NotificationCulture);

                    var emailSubject = nc.CultureContents
                                       .First(p => p.ContentKey == subjectKey)
                                       .FormatContent(issue.FullId, displayname);

                    var bodyContent = nc.CultureContents
                                      .First(p => p.ContentKey == bodyKey)
                                      .TransformContent(data);

                    var message = new MailMessage()
                    {
                        Subject    = emailSubject,
                        Body       = bodyContent,
                        IsBodyHtml = true
                    };

                    mailService.Send(user.Email, message);
                }
                catch (Exception ex)
                {
                    ProcessException(ex);
                }
            }
        }
        /// <summary>
        /// Sends an email to the user that is assigned to the issue
        /// </summary>
        /// <param name="notification"></param>
        public static void SendNewAssigneeNotification(IssueNotification notification)
        {
            if (notification == null)
            {
                throw (new ArgumentNullException("notification"));
            }
            if (notification.IssueId <= Globals.NEW_ID)
            {
                throw (new ArgumentOutOfRangeException("notification", "The issue id is not valid for this notification"));
            }

            // TODO - create this via dependency injection at some point.
            IMailDeliveryService mailService = new SmtpMailDeliveryService();

            var issue           = DataProviderManager.Provider.GetIssueById(notification.IssueId);
            var emailFormatType = HostSettingManager.Get(HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);

            // data for template
            var data = new Dictionary <string, object> {
                { "Issue", issue }
            };
            var          emailFormatKey = (emailFormatType == EmailFormatType.Text) ? "" : "HTML";
            const string subjectKey     = "NewAssigneeSubject";
            var          bodyKey        = string.Concat("NewAssignee", emailFormatKey);

            var nc = new CultureNotificationContent().LoadContent(notification.NotificationCulture, subjectKey, bodyKey);

            try
            {
                //send notifications to everyone except who changed it.
                if (notification.NotificationUsername.ToLower() == Security.GetUserName().ToLower())
                {
                    return;
                }

                var user = UserManager.GetUser(notification.NotificationUsername);

                // skip to the next user if this user is not approved
                if (!user.IsApproved)
                {
                    return;
                }
                // skip to next user if this user doesn't have notifications enabled.
                if (!new WebProfile().GetProfile(user.UserName).ReceiveEmailNotifications)
                {
                    return;
                }

                var emailSubject = nc.CultureContents
                                   .First(p => p.ContentKey == subjectKey)
                                   .FormatContent(issue.FullId);

                var bodyContent = nc.CultureContents
                                  .First(p => p.ContentKey == bodyKey)
                                  .TransformContent(data);

                var message = new MailMessage
                {
                    Subject    = emailSubject,
                    Body       = bodyContent,
                    IsBodyHtml = true
                };

                mailService.Send(user.Email, message);
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }