Account registration email confirmation by api.

GTEK IT DEV Team asked on July 9, 2019 12:04

I have a code snippet to add new user.

// Creates a new user object
UserInfo newUser = new UserInfo();

// Sets the user properties
newUser.FullName = firstName + " " + lastName;
newUser.UserName = StringUltils.RemoveSign4VietnameseString(userName.ToLower());
newUser.PasswordFormat = password;
newUser.Email = email;
newUser.PreferredCultureCode = cultureInfo.CultureCode;
newUser.UserSettings.UserWaitingForApproval = true;
newUser.SetValue("Organisation", organisation);

// Sets the user's privilege level to 'Editor'
newUser.SiteIndependentPrivilegeLevel = UserPrivilegeLevelEnum.None;

// Saves the user to the database
UserInfoProvider.SetUserInfo(newUser);
UserInfoProvider.SetPassword(newUser, password);
UserInfoProvider.AddUserToSite(newUser.UserName, SiteContext.CurrentSiteName);

And i also turn on 2 options: - Registration requires email confirmation - Registration requires administrator's approval

But after run the code above, i just see the user in awaiting for approval list. And i didn't see any email confirmation. What i'm missing?. Thanks.

Correct Answer

Dmitry Bastron answered on July 9, 2019 13:09

Hi Anh,

You need to trigger this email manually from the code. For a correct example you can refer to \CMS\CMSWebParts\Membership\Registration\RegistrationForm.ascx.cs webpart code. There you can find the following section:

#region "Welcome Emails (confirmation, waiting for approval)"

bool error = false;
EmailTemplateInfo template = null;

string emailSubject = null;
// Send welcome message with username and password, with confirmation link, user must confirm registration
if (requiresConfirmation)
{
    template = EmailTemplateProvider.GetEmailTemplate("RegistrationConfirmation", siteName);
    emailSubject = EmailHelper.GetSubject(template, GetString("RegistrationForm.RegistrationConfirmationEmailSubject"));
}
// Send welcome message with username and password, with information that user must be approved by administrator
else if (SendWelcomeEmail)
{
    if (requiresAdminApprove)
    {
        template = EmailTemplateProvider.GetEmailTemplate("Membership.RegistrationWaitingForApproval", siteName);
        emailSubject = EmailHelper.GetSubject(template, GetString("RegistrationForm.RegistrationWaitingForApprovalSubject"));
    }
    // Send welcome message with username and password, user can logon directly
    else
    {
        template = EmailTemplateProvider.GetEmailTemplate("Membership.Registration", siteName);
        emailSubject = EmailHelper.GetSubject(template, GetString("RegistrationForm.RegistrationSubject"));
    }
}

if (template != null)
{
    // Create relation between contact and user. This ensures that contact will be correctly recognized when user approves registration (if approval is required)
    // New contact with user data is created in case it is not disabled by Data protection settings, otherwise an anonymous contact is created. Moreover an another  
    // anonymous contact could be created during approval process when a new browser is used for approval step (when contact isn't in browser cookies).
    int contactId = ModuleCommands.OnlineMarketingGetCurrentContactID();
    if (contactId > 0)
    {
        Service.Resolve<IContactRelationAssigner>().Assign(ui.UserID, MembershipType.CMS_USER, contactId, new UserContactDataPropagationChecker());
    }

    // Email message
    EmailMessage email = new EmailMessage();
    email.EmailFormat = EmailFormatEnum.Default;
    email.Recipients = ui.Email;
    email.From = SettingsKeyInfoProvider.GetValue(siteName + ".CMSNoreplyEmailAddress");
    email.Subject = emailSubject;

    try
    {
        var resolver = MembershipResolvers.GetMembershipRegistrationResolver(ui, AuthenticationHelper.GetRegistrationApprovalUrl(ApprovalPage, ui.UserGUID, siteName, NotifyAdministrator));
        EmailSender.SendEmailWithTemplateText(siteName, email, template, resolver, true);
    }
    catch (Exception ex)
    {
        EventLogProvider.LogException("E", "RegistrationForm - SendEmail", ex);
        error = true;
    }
}

// If there was some error, user must be deleted
if (error)
{
    ShowError(GetString("RegistrationForm.UserWasNotCreated"));

    // Email was not send, user can't be approved - delete it
    UserInfoProvider.DeleteUser(ui);
    return;
}

#endregion
0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.