/** Since Openbravo 3.0MP9 only {@link #sendEmail()} is used for the full email sending cycle */
  @Deprecated
  public static Session newMailSession(
      ConnectionProvider connectionProvider, String clientId, String adOrgId)
      throws PocException, ServletException {
    PocConfigurationData configurations[];
    try {
      configurations = PocConfigurationData.getSmtpDetails(connectionProvider, clientId, adOrgId);
    } catch (ServletException exception) {
      throw new PocException(exception);
    }

    PocConfigurationData configuration = null;
    if (configurations.length > 0) {
      configuration = configurations[0];
      if (log4j.isDebugEnabled()) {
        log4j.debug("Crm configuration, smtp server: " + configuration.smtpserver);
        log4j.debug("Crm configuration, smtp server auth: " + configuration.issmtpauthorization);
        log4j.debug("Crm configuration, smtp server account: " + configuration.smtpserveraccount);
        log4j.debug("Crm configuration, smtp server password: "******"Crm configuration, smtp server connection security: "
                + configuration.smtpconnectionsecurity);
        log4j.debug("Crm configuration, smtp server port: " + configuration.smtpport);
      }
    } else {
      throw new ServletException("No Poc configuration found for this client.");
    }

    Properties props = new Properties();

    if (log4j.isDebugEnabled()) {
      props.put("mail.debug", "true");
    }
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.host", configuration.smtpserver);
    props.put("mail.smtp.auth", (configuration.issmtpauthorization.equals("Y") ? "true" : "false"));
    props.put("mail.smtp.mail.sender", "*****@*****.**");
    props.put("mail.smtp.port", configuration.smtpport);
    if (configuration.smtpconnectionsecurity.equals("STARTTLS")) {
      props.put("mail.smtp.starttls.enable", "true");
    } else if (configuration.smtpconnectionsecurity.equals("SSL")) {
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.socketFactory.port", configuration.smtpport);
    }

    ClientAuthenticator authenticator = null;
    if (configuration.smtpserveraccount != null) {
      authenticator =
          new ClientAuthenticator(
              configuration.smtpserveraccount,
              FormatUtilities.encryptDecrypt(configuration.smtpserverpassword, false));
    }

    return Session.getInstance(props, authenticator);
  }
  /**
   * Checks the Internet availability and sets the proxy in case it is needed.
   *
   * @deprecated Proxy settings should not be passed as parameter, but obtained from system
   *     information. Use instead {@link HttpsUtils#isInternetAvailable()}
   * @param proxyHost
   * @param proxyPort
   */
  public static boolean isInternetAvailable(String proxyHost, int proxyPort) {
    OBContext.setAdminMode();
    try {
      final SystemInformation sys = OBDal.getInstance().get(SystemInformation.class, "0");
      if (sys.isProxyRequired() || (proxyHost != null && !proxyHost.isEmpty())) {
        // Proxy is required for connection.
        String host;
        int port;
        if (proxyHost == null || proxyHost.isEmpty()) {
          // to maintain backwards compatibility, set host in case it is provided as parameter (it
          // shouldn't be)
          host = sys.getProxyServer();
          port = sys.getProxyPort().intValue();
        } else {
          host = proxyHost;
          port = proxyPort;
        }
        System.getProperties().put("proxySet", "true");
        System.getProperties().put("http.proxyHost", host);
        System.getProperties().put("https.proxyHost", host);
        System.getProperties().put("http.proxyPort", String.valueOf(port));
        System.getProperties().put("https.proxyPort", String.valueOf(port));

        System.setProperty("java.net.useSystemProxies", "true");

        if (sys.isRequiresProxyAuthentication()) {
          final String user = sys.getProxyUser();
          String pass = "";
          try {
            pass = FormatUtilities.encryptDecrypt(sys.getProxyPassword(), false);
          } catch (ServletException e) {
            log4j.error("Error setting proxy authenticator", e);
          }
          final String password = pass;

          // Used for standard http and https connections
          Authenticator.setDefault(
              new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(user, password.toCharArray());
                }
              });

          // Used for SOAP webservices
          System.getProperties().setProperty("http.proxyUser", user);
          System.getProperties().setProperty("http.proxyPassword", password);
        }
      } else {
        System.getProperties().put("proxySet", false);
        System.getProperties().remove("http.proxyHost");
        System.getProperties().remove("http.proxyPort");
        System.getProperties().remove("https.proxyHost");
        System.getProperties().remove("https.proxyPort");
        System.getProperties().remove("http.proxyUser");
        System.getProperties().remove("http.proxyPassword");
        System.setProperty("java.net.useSystemProxies", "false");
      }
    } finally {
      OBContext.restorePreviousMode();
    }

    try {
      // Double check.
      URL url = new URL("https://butler.openbravo.com");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setConnectTimeout(3000);
      conn.connect();
      if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        return false;
      }
    } catch (Exception e) {
      log4j.info("Unable to reach butler.openbravo.com");
      return false;
    }
    return true;
  }