Email createEmailWithBody(EmailEventNotificationConfig config, Map<String, Object> model) throws EmailException {
        if (!isNullOrEmpty(config.htmlBodyTemplate())) {
            HtmlEmail email = emailFactory.htmlEmail();
            email.setTextMsg(buildBody(config, model));
            email.setHtmlMsg(buildHtmlBody(config, model));
            return email;
        } else {
            SimpleEmail email = emailFactory.simpleEmail();
            email.setMsg(buildBody(config, model));
            return email;
        }
    }
@Test
    void testEmailHtmlEscaping() throws EmailException {
        Map<String, Object> model = Map.of(
                "event_definition_title", "<<Test Event Title>>",
                "event", Map.of("message", "Event Message & Whatnot")
        );
        final EmailEventNotificationConfig config = EmailEventNotificationConfig.builder()
                .htmlBodyTemplate(
                    "Message:              ${event.message}\n" +
                    "Title:                ${event_definition_title}\n"
                )
                .build();

        emailSender.createEmailWithBody(config, model);

        final ArgumentCaptor<String> plainCaptor = ArgumentCaptor.forClass(String.class);
        final ArgumentCaptor<String> htmlCaptor = ArgumentCaptor.forClass(String.class);
        verify(htmlEmail).setTextMsg(plainCaptor.capture());
        verify(htmlEmail).setHtmlMsg(htmlCaptor.capture());

        assertThat(plainCaptor.getValue()).matches(Pattern.compile(".*Title:\\s+<<Test Event Title>>.*", Pattern.DOTALL));
        assertThat(plainCaptor.getValue()).matches(Pattern.compile(".*Message:\\s+Event Message & Whatnot.*", Pattern.DOTALL));

        assertThat(htmlCaptor.getValue()).matches(Pattern.compile(".*Title:\\s+&lt;&lt;Test Event Title&gt;&gt;.*", Pattern.DOTALL));
        assertThat(htmlCaptor.getValue()).matches(Pattern.compile(".*Message:\\s+Event Message &amp; Whatnot.*", Pattern.DOTALL));
    }