EmailInviteSender implements EventSubscriber<InviteCreatedEvent> { public void sendEmail(String initiatorId, Invite invite) throws ServerException { String email = invite.getEmail(); String bearerToken = tokenHandler.generateBearerToken(email, new HashMap<>()); String joinLink = apiEndpoint.replace("/api", "") + "/site/auth/create?bearertoken=" + bearerToken; String initiator = getInitiatorInfo(initiatorId); Template template; String subject; switch (invite.getDomainId()) { case OrganizationDomain.DOMAIN_ID: template = new MemberInvitationTemplate(initiator, joinLink); subject = organizationInviteSubject; break; case WorkspaceDomain.DOMAIN_ID: template = new WorkerInvitationTemplate(initiator, joinLink); subject = workspaceInviteSubject; break; default: throw new ServerException( format( "There is no configured template for specified %s domain", invite.getDomainId())); } try { mailSender.sendAsync( resourceResolver.resolve( new EmailBean() .withSubject(subject) .withBody(templateProcessor.process(template)) .withFrom(mailFrom) .withReplyTo(mailFrom) .withTo(email) .withMimeType(TEXT_HTML))); } catch (TemplateException e) { throw new ServerException(e.getMessage(), e); } } @Inject  EmailInviteSender(
      @Named("che.api") String apiEndpoint,
      @Named("che.mail.from_email_address") String mailFrom,
      @Named("workspace.email.invite.subject") String workspaceInviteSubject,
      @Named("organization.email.invite.subject") String organizationInviteSubject,
      DefaultEmailResourceResolver resourceResolver,
      MailSender mailSender,
      UserManager userManager,
      ProfileManager profileManager,
      BearerTokenAuthenticationHandler tokenHandler,
      TemplateProcessor templateProcessor); @Inject void subscribe(EventService eventService); @Override void onEvent(InviteCreatedEvent event); void sendEmail(String initiatorId, Invite invite);  }
@Test(expectedExceptions = ServerException.class) public void shouldThrowServerExceptionWhenSpecifiedUnsupportedDomain() throws Exception { emailSender.sendEmail( USER_INITIATOR_ID, DtoFactory.newDto(InviteDto.class) .withDomainId("unsupported") .withInstanceId("instance123") .withEmail("user@test.com")); }