Email class
예제 #1
0
        protected void btnSend_Click( object sender, EventArgs e )
        {
            PersonService personService = new PersonService();

            var mergeObjects = new List<object>();

            var values = new Dictionary<string, string>();
            values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
            mergeObjects.Add( values );

            Dictionary<object, List<object>> personObjects = new Dictionary<object, List<object>>();

            foreach(Person person in personService.GetByEmail(tbEmail.Text))
            {
                var userObjects = new List<object>();

                UserService userService = new UserService();
                foreach ( User user in userService.GetByPersonId( person.Id ) )
                    if ( user.AuthenticationType != AuthenticationType.Facebook )
                        userObjects.Add( user );

                if ( userObjects.Count > 0 )
                    personObjects.Add( person, userObjects );
            }

            if ( personObjects.Count > 0 )
            {
                mergeObjects.Add( personObjects );

                var recipients = new Dictionary<string, List<object>>();
                recipients.Add( tbEmail.Text, mergeObjects );

                Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_FORGOT_USERNAME );
                SetSMTPParameters( email );
                email.Send( recipients );

                pnlEntry.Visible = false;
                pnlSuccess.Visible = true;
            }
            else
                pnlWarning.Visible = true;
        }
예제 #2
0
파일: SendEmail.cs 프로젝트: pkdevbox/Rock
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute( WorkflowAction action, Object entity, out List<string> errorMessages )
        {
            errorMessages = new List<string>();

            var recipientEmail = GetAttributeValue( action, "Recipient" );
            var recipients = new Dictionary<string, Dictionary<string, object>>();
            var mergeObjects = new Dictionary<string, object>();

            if ( entity != null )
            {
                mergeObjects.Add( entity.GetType().Name, entity );
            }

            recipients.Add( recipientEmail, mergeObjects );

            Email email = new Email( new System.Guid( GetAttributeValue( action, "EmailTemplate" ) ) );
            email.Send( recipients );

            action.AddLogEntry( string.Format( "Email sent to '{0}'", recipientEmail ) );
            
            return true;
        }
예제 #3
0
        // default error handling
        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Application_Error( object sender, EventArgs e )
        {
            HttpContext context = HttpContext.Current;

            // If the current context is null, there's nothing that can be done. Just return.
            if ( context == null )
            {
                return;
            }

            // log error
            var ex = Context.Server.GetLastError();

            if ( ex != null )
            {
                bool logException = true;

                // string to send a message to the error page to prevent infinite loops
                // of error reporting from incurring if there is an exception on the error page
                string errorQueryParm = "?type=exception&error=1";

                string errorCount = context.Request["error"];
                if ( !string.IsNullOrWhiteSpace( errorCount ) )
                {
                    if ( errorCount == "1" )
                    {
                        errorQueryParm = "?type=exception&error=2";
                    }
                    else if ( errorCount == "2" )
                    {
                        // something really bad is occurring stop logging errors as we're in an infinate loop
                        logException = false;
                    }
                }

                if ( logException )
                {
                    string status = "500";

                    var globalAttributesCache = GlobalAttributesCache.Read();

                    // determine if 404's should be tracked as exceptions
                    bool track404 = Convert.ToBoolean( globalAttributesCache.GetValue( "Log404AsException" ) );

                    // set status to 404
                    if ( ex.Message == "File does not exist." && ex.Source == "System.Web" )
                    {
                        status = "404";
                    }

                    if ( status == "500" || track404 )
                    {
                        LogError( ex, context );
                        context.Server.ClearError();

                        string errorPage = string.Empty;

                        // determine error page based on the site
                        SiteService service = new SiteService();
                        string siteName = string.Empty;

                        if ( context.Items["Rock:SiteId"] != null )
                        {
                            int siteId;
                            Int32.TryParse( context.Items["Rock:SiteId"].ToString(), out siteId );

                            // load site
                            Site site = service.Get( siteId );
                            siteName = site.Name;
                            errorPage = site.ErrorPage;
                        }

                        // Attempt to store exception in session. Session state may not be available
                        // within the context of an HTTP handler or the REST API.
                        try { Session["Exception"] = ex; }
                        catch ( HttpException ) { }

                        // email notifications if 500 error
                        if ( status == "500" )
                        {
                            // setup merge codes for email
                            var mergeObjects = new Dictionary<string, object>();
                            mergeObjects.Add( "ExceptionDetails", "An error occurred on the " + siteName + " site on page: <br>" + context.Request.Url.OriginalString + "<p>" + FormatException( ex, "" ) );

                            // get email addresses to send to
                            string emailAddressesList = globalAttributesCache.GetValue( "EmailExceptionsList" );

                            if ( !string.IsNullOrWhiteSpace( emailAddressesList ) )
                            {
                                string[] emailAddresses = emailAddressesList.Split( new[] { ',' }, StringSplitOptions.RemoveEmptyEntries );
                                var recipients = new Dictionary<string, Dictionary<string, object>>();

                                foreach ( string emailAddress in emailAddresses )
                                {
                                    recipients.Add( emailAddress, mergeObjects );
                                }

                                if ( recipients.Count > 0 )
                                {
                                    Email email = new Email( Rock.SystemGuid.EmailTemplate.CONFIG_EXCEPTION_NOTIFICATION );
                                    email.Send( recipients );
                                }
                            }
                        }

                        // redirect to error page
                        if ( !string.IsNullOrEmpty( errorPage ) )
                        {
                            Response.Redirect( errorPage + errorQueryParm, false );
                            Context.ApplicationInstance.CompleteRequest();
                        }
                        else
                        {
                            Response.Redirect( "~/error.aspx" + errorQueryParm, false );  // default error page
                            Context.ApplicationInstance.CompleteRequest();
                        }

                        // intentially throw ThreadAbort
                        Response.End();
                    }
                }
            }
        }
예제 #4
0
        // default error handling
        protected void Application_Error( object sender, EventArgs e )
        {
            // log error
            System.Web.HttpContext context = HttpContext.Current;
            System.Exception ex = Context.Server.GetLastError();

            bool logException = true;

            // string to send a message to the error page to prevent infinite loops
            // of error reporting from incurring if there is an exception on the error page
            string errorQueryParm = "?error=1";

            if (context.Request.Url.ToString().Contains("?error=1"))
            {
                errorQueryParm = "?error=2";
            }
            else if ( context.Request.Url.ToString().Contains( "?error=2" ) )
            {
                // something really bad is occurring stop logging errors as we're in an infinate loop
                logException = false;
            }

            if ( logException )
            {
                string status = "500";

                // determine if 404's should be tracked as exceptions
                bool track404 = Convert.ToBoolean( Rock.Web.Cache.GlobalAttributes.Value( "Log404AsException" ) );

                // set status to 404
                if ( ex.Message == "File does not exist." && ex.Source == "System.Web" )
                {
                    status = "404";
                }

                if (status == "500" || track404)
                {
                    LogError( ex, -1, status, context );
                    context.Server.ClearError();

                    string errorPage = string.Empty;

                    // determine error page based on the site
                    SiteService service = new SiteService();
                    Site site = null;
                    string siteName = string.Empty;

                    if ( context.Items["Rock:SiteId"] != null )
                    {
                        int siteId = Int32.Parse( context.Items["Rock:SiteId"].ToString() );

                        // load site
                        site = service.Get( siteId );

                        siteName = site.Name;
                        errorPage = site.ErrorPage;
                    }

                    // store exception in session
                    Session["Exception"] = ex;

                    // email notifications if 500 error
                    if ( status == "500" )
                    {
                        // setup merge codes for email
                        var mergeObjects = new List<object>();

                        var values = new Dictionary<string, string>();

                        string exceptionDetails = "An error occurred on the " + siteName + " site on page: <br>" + context.Request.Url.OriginalString + "<p>" + FormatException( ex, "" );
                        values.Add( "ExceptionDetails", exceptionDetails );
                        mergeObjects.Add( values );

                        // get email addresses to send to
                        string emailAddressesList = Rock.Web.Cache.GlobalAttributes.Value( "EmailExceptionsList" );
                        if ( emailAddressesList != null )
                        {
                            string[] emailAddresses = emailAddressesList.Split( new char[] { ',' } );

                            var recipients = new Dictionary<string, List<object>>();

                            foreach ( string emailAddress in emailAddresses )
                            {
                                recipients.Add( emailAddress, mergeObjects );
                            }

                            if ( recipients.Count > 0 )
                            {
                                Email email = new Email( Rock.SystemGuid.EmailTemplate.CONFIG_EXCEPTION_NOTIFICATION );
                                SetSMTPParameters( email );  //TODO move this set up to the email object
                                email.Send( recipients );
                            }
                        }
                    }

                    // redirect to error page
                    if ( errorPage != null && errorPage != string.Empty )
                        Response.Redirect( errorPage + errorQueryParm );
                    else
                        Response.Redirect( "~/error.aspx" + errorQueryParm );  // default error page
                }
            }
        }
예제 #5
0
        private void SetSMTPParameters( Email email )
        {
            email.Server = Rock.Web.Cache.GlobalAttributes.Value( "SMTPServer" );

            int port = 0;
            if ( !Int32.TryParse( Rock.Web.Cache.GlobalAttributes.Value( "SMTPPort" ), out port ) )
                port = 0;
            email.Port = port;

            bool useSSL = false;
            if ( !bool.TryParse( Rock.Web.Cache.GlobalAttributes.Value( "SMTPUseSSL" ), out useSSL ) )
                useSSL = false;
            email.UseSSL = useSSL;

            email.UserName = Rock.Web.Cache.GlobalAttributes.Value( "SMTPUserName" );
            email.Password = Rock.Web.Cache.GlobalAttributes.Value( "SMTPPassword" );
        }
예제 #6
0
        protected void btnSend_Click( object sender, EventArgs e )
        {
            var mergeObjects = new Dictionary<string, object>();

            var url = LinkedPageUrl( "ConfirmationPage" );
            if ( string.IsNullOrWhiteSpace( url ) )
            {
                url = ResolveRockUrl( "~/ConfirmAccount" );
            }
            mergeObjects.Add( "ConfirmAccountUrl", RootPath + url.TrimStart( new char[] { '/' } ) );

            var personDictionaries = new List<IDictionary<string, object>>();

            var personService = new PersonService();
            var userLoginService = new UserLoginService();

            foreach ( Person person in personService.GetByEmail( tbEmail.Text ) )
            {
                var users = new List<IDictionary<string,object>>();
                foreach ( UserLogin user in userLoginService.GetByPersonId( person.Id ) )
                {
                    if ( user.EntityType != null )
                    {
                        var component = AuthenticationContainer.GetComponent( user.EntityType.Name );
                        if ( component.ServiceType == AuthenticationServiceType.Internal )
                        {
                            users.Add( user.ToDictionary() );
                        }
                    }
                }

                if (users.Count > 0)
                {
                    IDictionary<string,object> personDictionary = person.ToDictionary();
                    personDictionary.Add("Users", users.ToArray());
                    personDictionaries.Add( personDictionary );
                }
            }

            if ( personDictionaries.Count > 0 )
            {
                mergeObjects.Add( "Persons", personDictionaries.ToArray() );

                var recipients = new Dictionary<string, Dictionary<string, object>>();
                recipients.Add( tbEmail.Text, mergeObjects );

                Email email = new Email( GetAttributeValue( "EmailTemplate" ) );
                email.Send( recipients );

                pnlEntry.Visible = false;
                pnlSuccess.Visible = true;
            }
            else
                pnlWarning.Visible = true;
        }
예제 #7
0
        private void DisplaySuccess( Rock.Model.UserLogin user )
        {
            FormsAuthentication.SignOut();
            Rock.Security.Authorization.SetAuthCookie( tbUserName.Text, false, false );

            if ( user != null && user.PersonId.HasValue )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( user.PersonId.Value );

                if ( person != null )
                {
                    string url = LinkedPageUrl( "ConfirmationPage" );
                    if ( string.IsNullOrWhiteSpace( url ) )
                    {
                        url = ResolveRockUrl( "~/ConfirmAccount" );
                    }
                    var mergeObjects = new Dictionary<string, object>();
                    mergeObjects.Add( "ConfirmAccountUrl", RootPath + url.TrimStart( new char[] { '/' } ) );

                    var personDictionary = person.ToDictionary();
                    mergeObjects.Add( "Person", personDictionary );

                    mergeObjects.Add( "User", user.ToDictionary() );

                    var recipients = new Dictionary<string, Dictionary<string, object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( GetAttributeValue( "AccountCreatedTemplate" ) );
                    email.Send( recipients );

                    lSuccessCaption.Text = GetAttributeValue( "SuccessCaption" );
                    if ( lSuccessCaption.Text.Contains( "{0}" ) )
                        lSuccessCaption.Text = string.Format( lSuccessCaption.Text, person.FirstName );

                    ShowPanel( 5 );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }
            else
                ShowErrorMessage( "Invalid User" );
        }
예제 #8
0
        private void DisplayConfirmation( int personId )
        {
            PersonService personService = new PersonService();
            Person person = personService.Get(personId);

            if (person != null)
            {
                Rock.Model.UserLogin user = CreateUser( person, false );

                string url = LinkedPageUrl( "ConfirmationPage" );
                if ( string.IsNullOrWhiteSpace( url ) )
                {
                    url = ResolveRockUrl( "~/ConfirmAccount" );
                }
                var mergeObjects = new Dictionary<string, object>();
                mergeObjects.Add( "ConfirmAccountUrl", RootPath + url.TrimStart( new char[] { '/' } ) ); 
                
                var personDictionary = person.ToDictionary();
                mergeObjects.Add( "Person", personDictionary );

                mergeObjects.Add( "User", user.ToDictionary() );

                var recipients = new Dictionary<string, Dictionary<string, object>>();
                recipients.Add( person.Email, mergeObjects );

                Email email = new Email( GetAttributeValue( "ConfirmAccountTemplate" ) );
                email.Send( recipients );

                ShowPanel( 4 );
            }
            else
                ShowErrorMessage("Invalid Person");
        }
예제 #9
0
        private void DisplaySentLogin( Direction direction )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( Int32.Parse( hfSendPersonId.Value ) );
                if ( person != null )
                {
                    string url = LinkedPageUrl( "ConfirmationPage" );
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        url = ResolveRockUrl( "~/ConfirmAccount" );
                    }
                    var mergeObjects = new Dictionary<string, object>();
                    mergeObjects.Add( "ConfirmAccountUrl", RootPath + url.TrimStart(new char[]{'/'}) );

                    var personDictionaries = new List<IDictionary<string, object>>();

                    var users = new List<IDictionary<string, object>>();
                    var userLoginService = new UserLoginService();
                    foreach ( UserLogin user in userLoginService.GetByPersonId( person.Id ) )
                    {
                        if ( user.EntityType != null )
                        {
                            var component = AuthenticationContainer.GetComponent( user.EntityType.Name );
                            if ( component.ServiceType == AuthenticationServiceType.Internal )
                            {
                                users.Add( user.ToDictionary() );
                            }
                        }
                    }

                    if ( users.Count > 0 )
                    {
                        IDictionary<string, object> personDictionary = person.ToDictionary();
                        personDictionary.Add( "Users", users.ToArray() );
                        personDictionaries.Add( personDictionary );
                    }

                    mergeObjects.Add( "Persons", personDictionaries.ToArray() );

                    var recipients = new Dictionary<string, Dictionary<string, object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( GetAttributeValue( "ForgotUsernameTemplate" ) );
                    email.Send( recipients );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }

            ShowPanel( 3 );
        }
예제 #10
0
        private void DisplaySuccess( Rock.Model.UserLogin user )
        {
            FormsAuthentication.SignOut();
            Rock.Security.Authorization.SetAuthCookie( tbUserName.Text, false, false );

            if ( user != null && user.PersonId.HasValue )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( user.PersonId.Value );

                if ( person != null )
                {
                    var mergeObjects = new Dictionary<string, object>();
                    mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );

                    var personDictionary = new PersonDto( person ).ToDictionary();
                    personDictionary.Add("FirstName", person.FirstName);
                    mergeObjects.Add( "Person", personDictionary );

                    mergeObjects.Add( "User", new UserLoginDto( user ).ToDictionary() );

                    var recipients = new Dictionary<string, Dictionary<string, object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_ACCOUNT_CREATED );
                    email.Send( recipients );

                    lSuccessCaption.Text = AttributeValue( "SuccessCaption" );
                    if ( lSuccessCaption.Text.Contains( "{0}" ) )
                        lSuccessCaption.Text = string.Format( lSuccessCaption.Text, person.FirstName );

                    ShowPanel( 5 );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }
            else
                ShowErrorMessage( "Invalid User" );
        }
예제 #11
0
        private void DisplaySentLogin( Direction direction )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( Int32.Parse( hfSendPersonId.Value ) );
                if ( person != null )
                {
                    var mergeObjects = new Dictionary<string, object>();
                    mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );

                    var personDictionaries = new List<IDictionary<string, object>>();

                    var users = new List<IDictionary<string, object>>();
                    UserService userService = new UserService();
                    foreach ( UserLogin user in userService.GetByPersonId( person.Id ) )
                    {
                        if ( user.ServiceType == AuthenticationServiceType.Internal )
                        {
                            var userDictionary = new UserLoginDto( user ).ToDictionary();
                            userDictionary.Add( "ConfirmationCodeEncoded", user.ConfirmationCodeEncoded );
                            users.Add( userDictionary );
                        }
                    }

                    if ( users.Count > 0 )
                    {
                        IDictionary<string, object> personDictionary = new PersonDto( person ).ToDictionary();
                        personDictionary.Add( "FirstName", person.FirstName );
                        personDictionary.Add( "Users", users.ToArray() );
                        personDictionaries.Add( personDictionary );
                    }

                    mergeObjects.Add( "Persons", personDictionaries.ToArray() );

                    var recipients = new Dictionary<string, Dictionary<string, object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_FORGOT_USERNAME );
                    email.Send( recipients );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }

            ShowPanel( 3 );
        }
예제 #12
0
        private void DisplayConfirmation( int personId )
        {
            PersonService personService = new PersonService();
            Person person = personService.Get(personId);

            if (person != null)
            {
                Rock.Model.UserLogin user = CreateUser( person, false );

                var mergeObjects = new Dictionary<string, object>();
                mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );

                var personDictionary = new PersonDto( person ).ToDictionary();
                personDictionary.Add( "FirstName", person.FirstName );
                mergeObjects.Add( "Person", personDictionary );

                mergeObjects.Add( "User", new UserLoginDto( user ).ToDictionary() );

                var recipients = new Dictionary<string, Dictionary<string, object>>();
                recipients.Add( person.Email, mergeObjects );

                Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_CONFIRM_ACCOUNT );
                email.Send( recipients );

                ShowPanel( 4 );
            }
            else
                ShowErrorMessage("Invalid Person");
        }
예제 #13
0
        private void DisplaySuccess( Rock.CMS.User user )
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.SetAuthCookie( tbUserName.Text, false );

            if ( user != null && user.PersonId.HasValue )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( user.PersonId.Value );

                if ( person != null )
                {
                    var mergeObjects = new List<object>();
                    mergeObjects.Add( person );
                    mergeObjects.Add( user );

                    var values = new Dictionary<string, string>();
                    values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                    mergeObjects.Add( values );

                    var recipients = new Dictionary<string, List<object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_ACCOUNT_CREATED );
                    SetSMTPParameters( email );
                    email.Send( recipients );

                    lSuccessCaption.Text = AttributeValue( "SuccessCaption" );
                    if ( lSuccessCaption.Text.Contains( "{0}" ) )
                        lSuccessCaption.Text = string.Format( lSuccessCaption.Text, person.FirstName );

                    ShowPanel( 5 );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }
            else
                ShowErrorMessage( "Invalid User" );
        }
예제 #14
0
        private void DisplaySentLogin( Direction direction )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                PersonService personService = new PersonService();
                Rock.CMS.UserService userService = new Rock.CMS.UserService();

                Person person = personService.Get( Int32.Parse( hfSendPersonId.Value ) );
                if ( person != null )
                {
                    var mergeObjects = new List<object>();

                    var values = new Dictionary<string, string>();
                    values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                    mergeObjects.Add( values );

                    Dictionary<object, List<object>> personObjects = new Dictionary<object, List<object>>();
                    var userObjects = new List<object>();

                    mergeObjects.Add( person );

                    foreach ( var user in userService.GetByPersonId( person.Id ) )
                        if (user.AuthenticationType != Rock.CMS.AuthenticationType.Facebook)
                            userObjects.Add( user );

                    personObjects.Add( person, userObjects );
                    mergeObjects.Add(personObjects);

                    var recipients = new Dictionary<string, List<object>>();
                    recipients.Add(person.Email, mergeObjects);

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_FORGOT_USERNAME );
                    SetSMTPParameters( email );
                    email.Send( recipients );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }

            ShowPanel( 3 );
        }
예제 #15
0
        private void DisplayConfirmation( int personId )
        {
            PersonService personService = new PersonService();
            Person person = personService.Get(personId);

            if (person != null)
            {
                Rock.CMS.User user = CreateUser( person, false );

                var mergeObjects = new List<object>();
                mergeObjects.Add( person );
                mergeObjects.Add( user );

                var values = new Dictionary<string, string>();
                values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                mergeObjects.Add( values );

                var recipients = new Dictionary<string, List<object>>();
                recipients.Add( person.Email, mergeObjects );

                Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_CONFIRM_ACCOUNT );
                SetSMTPParameters( email );
                email.Send( recipients );

                ShowPanel( 4 );
            }
            else
                ShowErrorMessage("Invalid Person");
        }
예제 #16
0
        protected void btnSave_Click( object sender, EventArgs e )
        {
            if ( string.IsNullOrWhiteSpace( txtFirstName.Text ) ||
                string.IsNullOrWhiteSpace( txtLastName.Text ) ||
                string.IsNullOrWhiteSpace( txtEmail.Text ) )
            {
                ShowError( "Missing Information", "Please enter a value for First Name, Last Name, and Email" );
            }
            else
            {
                var person = GetPerson();
                if ( person != null )
                {
                    int groupId = int.MinValue;
                    if ( int.TryParse( GetAttributeValue( "Group" ), out groupId ) )
                    {
                        using ( new UnitOfWorkScope() )
                        {
                            var groupService = new GroupService();
                            var groupMemberService = new GroupMemberService();

                            var group = groupService.Get( groupId );
                            if ( group != null && group.GroupType.DefaultGroupRoleId.HasValue )
                            {
                                string linkedPage = GetAttributeValue( "ConfirmationPage" );
                                if ( !string.IsNullOrWhiteSpace( linkedPage ) )
                                {
                                    var member = group.Members.Where( m => m.PersonId == person.Id ).FirstOrDefault();

                                    // If person has not registered or confirmed their registration
                                    if ( member == null || member.GroupMemberStatus != GroupMemberStatus.Active )
                                    {
                                        Email email = null;

                                        Guid guid = Guid.Empty;
                                        if ( Guid.TryParse( GetAttributeValue( "ConfirmationEmail" ), out guid ) )
                                        {
                                            email = new Email( guid );
                                        }

                                        if ( member == null )
                                        {
                                            member = new GroupMember();
                                            member.GroupId = group.Id;
                                            member.PersonId = person.Id;
                                            member.GroupRoleId = group.GroupType.DefaultGroupRoleId.Value;

                                            // If a confirmation email is configured, set status to Pending otherwise set it to active
                                            member.GroupMemberStatus = email != null ? GroupMemberStatus.Pending : GroupMemberStatus.Active;

                                            groupMemberService.Add( member, CurrentPersonId );
                                            groupMemberService.Save( member, CurrentPersonId );
                                            member = groupMemberService.Get( member.Id );
                                        }

                                        // Send the confirmation
                                        if ( email != null )
                                        {
                                            var mergeObjects = new Dictionary<string, object>();
                                            mergeObjects.Add( "Member", member );

                                            var pageParams = new Dictionary<string, string>();
                                            pageParams.Add( "gm", member.UrlEncodedKey );
                                            var pageReference = new Rock.Web.PageReference( linkedPage, pageParams );
                                            mergeObjects.Add( "ConfirmationPage", pageReference.BuildUrl() );

                                            var recipients = new Dictionary<string, Dictionary<string, object>>();
                                            recipients.Add( person.Email, mergeObjects );
                                            email.Send( recipients );
                                        }

                                        ShowSuccess( GetAttributeValue( "SuccessMessage" ) );

                                    }
                                    else
                                    {
                                        var pageParams = new Dictionary<string, string>();
                                        pageParams.Add( "gm", member.UrlEncodedKey );
                                        var pageReference = new Rock.Web.PageReference( linkedPage, pageParams );
                                        Response.Redirect( pageReference.BuildUrl(), false );
                                    }
                                }
                                else
                                {
                                    ShowError( "Configuration Error", "Invalid Confirmation Page setting" );
                                }
                            }
                            else
                            {
                                ShowError( "Configuration Error",
                                    "The configured group does not exist, or it's group type does not have a default role configured." );
                            }
                        }
                    }
                    else
                    {
                        ShowError( "Configuration Error", "Invalid Group setting" );
                    }
                }
            }
        }