public void AddNotificationRecipient(IMailboxLocator recipient)
 {
     ArgumentValidator.ThrowIfNull("recipient", recipient);
     EmailNotificationHandler.Tracer.TraceDebug <string, IMailboxLocator>((long)this.GetHashCode(), "EmailNotificationHandler.AddNotificationRecipient: Queuing notification. Group={0}. Recipient={1}.", this.groupMailbox.ExternalDirectoryObjectId, recipient);
     if (!EmailNotificationHandler.IsDuplicateNotification(recipient, this.groupMailbox))
     {
         this.pendingRecipients.Enqueue(recipient);
     }
 }
        public void SetMembershipState(ADRecipient joinedBy, UserMailboxLocator[] joiningUsers, UserMailboxLocator[] departingUsers, GroupMailboxLocator group)
        {
            string joinedBy2 = string.Empty;

            if (joinedBy != null)
            {
                joinedBy2 = ((SmtpAddress)joinedBy[ADRecipientSchema.PrimarySmtpAddress]).ToString();
            }
            using (IAssociationStore associationStore = this.storeProviderBuilder.Create(group, this.PerformanceTracker))
            {
                ADUser groupMailbox = group.FindAdUser();
                WelcomeToGroupMessageTemplate        messageComposerBuilder  = new WelcomeToGroupMessageTemplate(groupMailbox, associationStore.MailboxOwner, joinedBy);
                EmailNotificationHandler             joinNotificationHandler = new EmailNotificationHandler(groupMailbox, associationStore.MailboxOwner, this.clientString, messageComposerBuilder);
                QueuedInProcessAssociationReplicator associationReplicator   = new QueuedInProcessAssociationReplicator(group, this.adSession, associationStore.ServerFullyQualifiedDomainName, this.clientString);
                try
                {
                    UserAssociationAdaptor userAssociationAdaptor = new UserAssociationAdaptor(associationStore, this.adSession, group);
                    if (departingUsers != null && departingUsers.Length > 0)
                    {
                        this.LogCommandExecution("LeaveGroup", group, departingUsers);
                        SetUserMembershipState setUserMembershipState = new SetUserMembershipState(this.Logger, associationReplicator, false, joinedBy2, userAssociationAdaptor, this.adSession, departingUsers);
                        setUserMembershipState.Execute();
                    }
                    if (joiningUsers != null && joiningUsers.Length > 0)
                    {
                        this.LogCommandExecution("JoinGroup", group, joiningUsers);
                        userAssociationAdaptor.OnAfterJoin += joinNotificationHandler.AddNotificationRecipient;
                        SetUserMembershipState setUserMembershipState2 = new SetUserMembershipState(this.Logger, associationReplicator, true, joinedBy2, userAssociationAdaptor, this.adSession, joiningUsers);
                        setUserMembershipState2.Execute();
                        userAssociationAdaptor.OnAfterJoin -= joinNotificationHandler.AddNotificationRecipient;
                    }
                }
                finally
                {
                    Task.Run(delegate()
                    {
                        associationReplicator.ReplicateQueuedAssociations();
                    }).ContinueWith(delegate(Task t)
                    {
                        joinNotificationHandler.SendNotification();
                    });
                }
            }
        }