public void CreateMailbox(UsersObject user) { CPDatabase database = null; ExchangePowershell powershell = null; CloudPanelTransaction transaction = new CloudPanelTransaction(); try { database = new CPDatabase(); // Get the user from the database var foundUser = (from u in database.Users where u.UserPrincipalName == user.UserPrincipalName select u).FirstOrDefault(); powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC); // Get the selected mailbox plan MailboxPlanObject mailboxPlan = GetMailboxPlan(user.MailboxPlan); // Create new mailbox and register transaction powershell.NewMailbox(user); transaction.NewMailbox(user.UserPrincipalName); // Update the mailbox values powershell.UpdateMailbox(user, mailboxPlan); powershell.UpdateCASMailbox(user, mailboxPlan); // Set litigation hold settings if enabled for litigation hold if (user.LitigationHoldEnabled) { powershell.NewLitigationHold(user.UserPrincipalName, user.LitigationHoldComment, user.LitigationHoldUrl, user.LitigationHoldDuration); } // Set archive settings if enabled for archiving if (user.ArchivingEnabled && user.ArchivePlan > 0) { powershell.NewArchiveMailbox(user); // Set quota on archive } foundUser.Email = user.PrimarySmtpAddress; foundUser.MailboxPlan = user.MailboxPlan; foundUser.AdditionalMB = user.SetMailboxSizeInMB - mailboxPlan.MailboxSizeInMB; foundUser.ExchArchivePlan = user.ArchivePlan; database.SaveChanges(); } catch (Exception ex) { this.logger.Error("Error creating mailbox for " + user.UserPrincipalName, ex); ThrowEvent(AlertID.FAILED, ex.Message); transaction.RollBack(); } finally { if (powershell != null) { powershell.Dispose(); } if (database != null) { database.Dispose(); } } }