@Override
  public boolean testEmail(String sid, String email) throws ServerException {
    UserSession session = ServiceUtil.validateSession(sid);

    ContextProperties config =
        (ContextProperties) Context.getInstance().getBean(ContextProperties.class);
    EMailSender sender = new EMailSender(session.getTenantName());

    try {
      EMail mail;
      mail = new EMail();
      mail.setAccountId(-1);
      mail.setAuthor(config.getProperty(session.getTenantName() + ".smtp.sender"));
      mail.setAuthorAddress(config.getProperty(session.getTenantName() + ".smtp.sender"));
      mail.parseRecipients(email);
      mail.setFolder("outbox");
      mail.setSentDate(new Date());
      mail.setSubject("test");
      mail.setMessageText("test");

      log.info("Sending test email to " + email);
      sender.send(mail);
      log.info("Test email sent");
      return true;
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return false;
    }
  }
  @Override
  public void saveEmailSettings(String sid, GUIEmailSettings settings) throws ServerException {
    UserSession session = ServiceUtil.validateSession(sid);

    try {
      ContextProperties conf =
          (ContextProperties) Context.getInstance().getBean(ContextProperties.class);

      conf.setProperty(session.getTenantName() + ".smtp.host", settings.getSmtpServer());
      conf.setProperty(
          session.getTenantName() + ".smtp.port", Integer.toString(settings.getPort()));
      conf.setProperty(
          session.getTenantName() + ".smtp.username",
          !settings.getUsername().trim().isEmpty() ? settings.getUsername() : "");
      conf.setProperty(
          session.getTenantName() + ".smtp.password",
          !settings.getPwd().trim().isEmpty() ? settings.getPwd() : "");
      conf.setProperty(
          session.getTenantName() + ".smtp.connectionSecurity", settings.getConnSecurity());
      conf.setProperty(
          session.getTenantName() + ".smtp.authEncripted",
          settings.isSecureAuth() ? "true" : "false");
      conf.setProperty(session.getTenantName() + ".smtp.sender", settings.getSenderEmail());

      conf.write();

      EMailSender sender = (EMailSender) Context.getInstance().getBean(EMailSender.class);
      sender.setHost(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.host"));
      sender.setPort(Integer.parseInt(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.port")));
      sender.setUsername(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.username"));
      sender.setPassword(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.password"));
      sender.setSender(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.sender"));
      sender.setAuthEncripted(
          "true".equals(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.authEncripted"))
              ? true
              : false);
      sender.setConnectionSecurity(
          Integer.parseInt(conf.getProperty(Tenant.DEFAULT_NAME + ".smtp.connectionSecurity")));

      log.info("Email settings data written successfully.");
    } catch (Exception e) {
      log.error("Exception writing Email settings data: " + e.getMessage(), e);
    }
  }