public void validateSmtp() { Validation.required("setup.smtpServer", smtpServer); if (HostNameOrIpAddressCheck.isValidHostName(smtpServer)) { if (PropertiesConfigurationValidator.validateIpList(nameservers)) { Set<String> ips = Sets.newHashSet(nameservers.split(",")); if (!DnsUtils.validateHostname(ips, smtpServer)) { Validation.addError("setup.smtpServer", "setup.smtpServer.invalidSmtpServer"); } } else if (StringUtils.isNotEmpty(nameservers)) { Validation.addError( "setup.nameservers", "setup.smtpServer.invalidNameserver", nameservers); } } if (!HostNameOrIpAddressCheck.isValidHostNameOrIp(smtpServer)) { Validation.addError("setup.smtpServer", "setup.smtpServer.invalid"); } if (!StringUtils.isNumeric(smtpPort)) { Validation.addError("setup.smtpServer", "setup.smtpServer.invalidPort"); } Validation.required("setup.smtpFrom", smtpFrom); Validation.email("setup.smtpFrom", smtpFrom); if (StringUtils.isNotBlank(smtpAuthType) && !StringUtils.equalsIgnoreCase(smtpAuthType, "None")) { Validation.required("setup.smtpUsername", smtpUsername); Validation.required("setup.smtpPassword", smtpPassword); if (PasswordUtil.isNotValid(smtpPassword)) { Validation.addError("setup.smtpPassword", "setup.password.notValid"); } } }
/** * Tests the SMTP settings from the initial setup form, rendering the result as JSON. * * @param setup the initial setup form. */ @Restrictions({ @Restrict({"SYSTEM_ADMIN", "SECURITY_ADMIN"}), @Restrict({"RESTRICTED_SYSTEM_ADMIN", "RESTRICTED_SECURITY_ADMIN"}) }) public static void testSmtpSettings(SetupForm setup) { setup.validateSmtp(); Validation.required("setup.smtpTo", setup.smtpTo); Validation.email("setup.smtpTo", setup.smtpTo); if (Validation.hasErrors()) { renderJSON(ValidationResponse.collectErrors()); } MailSettingsValidator.Settings settings = new MailSettingsValidator.Settings(); if (StringUtils.isNotEmpty(setup.nameservers) && !InetAddresses.isInetAddress(setup.smtpServer)) { Set<String> ips = Sets.newHashSet(setup.nameservers.split(",")); try { settings.server = DnsUtils.getHostIpAddress(ips, setup.smtpServer).getHostAddress(); } catch (ViPRException e) { renderJSON(ValidationResponse.invalid(e.getMessage())); } } else { settings.server = setup.smtpServer; } settings.port = ConfigProperties.getPort(setup.smtpPort, setup.smtpEnableTls); settings.username = setup.smtpUsername; settings.password = PasswordUtil.decryptedValue(setup.smtpPassword); settings.channel = StringUtils.equals("yes", setup.smtpEnableTls) ? "starttls" : "clear"; settings.authType = setup.smtpAuthType; settings.fromAddress = setup.smtpFrom; try { MailSettingsValidator.validate(settings, setup.smtpTo); } catch (RuntimeException e) { Logger.error(e, "Failed to send email"); Validation.addError(null, "setup.testEmail.failure", e.getMessage()); if (StringUtils.isEmpty(setup.nameservers)) { Validation.addError(null, "setup.smtpServer.invalidEmptyNameserver"); } } if (Validation.hasErrors()) { renderJSON(ValidationResponse.collectErrors()); } else { renderJSON(ValidationResponse.valid(MessagesUtils.get("setup.testEmail.success"))); } }