Sending an email breaks with NPE exception

I’m trying to send a custom email like this

    EmailTemplateProvider emailProvider = context.getSession().getProvider(EmailTemplateProvider.class);
    emailProvider.send(subject, emailTemplate, new HashMap<>());

But this breaks with the next exception Failed to template email
at org.keycloak.keycloak-services@15.0.2//
at org.keycloak.keycloak-services@15.0.2//
Caused by: java.lang.NullPointerException 
at org.keycloak.keycloak-services@15.0.2//

The said line (212 in processTemplate method) is


it’s not really clear from the exception why this actually happens

Do I need to configure the provider manually with the realm, locale etc.?