From 73ac6465b6f32150f1ff5ef488090a84b2dec87a Mon Sep 17 00:00:00 2001 From: MailboxValidator Date: Wed, 20 Mar 2024 16:06:45 +0800 Subject: [PATCH] Support new fields in MailboxValidator API --- src/com/mailboxvalidator/MBVResult.java | 362 +++++---- .../mailboxvalidator/SingleValidation.java | 720 +++++++++--------- 2 files changed, 561 insertions(+), 521 deletions(-) diff --git a/src/com/mailboxvalidator/MBVResult.java b/src/com/mailboxvalidator/MBVResult.java index ab0da5c..3201c56 100644 --- a/src/com/mailboxvalidator/MBVResult.java +++ b/src/com/mailboxvalidator/MBVResult.java @@ -1,167 +1,195 @@ -/** -* MBVResult.java -*/ - -package com.mailboxvalidator; - -public class MBVResult { - String email_address; - String domain; - String is_free; - String is_syntax; - String is_domain; - String is_smtp; - String is_verified; - String is_server_down; - String is_greylisted; - String is_disposable; - String is_suppressed; - String is_role; - String is_high_risk; - String is_catchall; - float mailboxvalidator_score; - float time_taken; - String status; - int credits_available; - String error_code; - String error_message; - String version = "2.0.0"; - MBVResult(String email) { - email_address = email; - } -/** - * This method to get the email address being validated. - * @return the email address being validated. - */ - public String getEmailAddress() { return email_address; } -/** - * This method to get the domain name of the email address. - * @return the domain name. - */ - public String getDomain() { return domain; } -/** - * This method to get whether the email address is from a free email provider. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsFree() { return is_free; } -/** - * This method to get whether the email address is syntactically correct. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsSyntax() { return is_syntax; } -/** - * This method to get whether the email domain has a valid MX record in the DNS entries. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsDomain() { return is_domain; } -/** - * This method to get whether the mail server is responding to connection. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsSMTP() { return is_smtp; } -/** - * This method to get whether the mail server confirms that the email address actually exists. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsVerified() { return is_verified; } -/** - * This method to get whether the mail server is currently down or unresponsive. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsServerDown() { return is_server_down; } -/** - * This method to get whether the mail server employs greylisting. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsGreylisted() { return is_greylisted; } -/** - * This method to get whether the email address is from a disposable email provider. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsDisposable() { return is_disposable; } -/** - * This method to get whether the email address is in our blacklist. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsSuppressed() { return is_suppressed; } -/** - * This method to get whether the email address is a role-based email address. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsRole() { return is_role; } -/** - * This method to get whether the email address contains high risk keywords. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsHighRisk() { return is_high_risk; } -/** - * This method to get whether the email address is a catch-all address. - * @return "true", "false" or "null" if not applicable. - */ - public String getIsCatchall() { return is_catchall; } -/** - * This method to get the email reputation score. - * @return the email reputation score; score greater than 0.70 means good; score greater than 0.40 means fair; score less than or equal to 0.40 means poor. - */ - public float getMailboxValidatorScore() { return mailboxvalidator_score; } -/** - * This method to get the execution time of the API in seconds. - * @return the time taken for the API execution in seconds. - */ - public float getTimeTaken() { return time_taken; } -/** - * This method to get whether our system think the email address is valid based on the various result fields. - * @return "true", "false" or "null" if not applicable. - */ - public String getStatus() { return status; } -/** - * This method to get the number of remaining query credits. - * @return the number of remaining query credits. - */ - public int getCreditsAvailable() { return credits_available; } -/** - * This method to get the error code. - * @return the error code if an error occurred or blank if none. - */ - public String getErrorCode() { return error_code; } -/** - * This method to get the error message. - * @return the error message if an error occurred or blank if none. - */ - public String getErrorMessage() { return error_message; } -/** - * This method to get component version. - * @return the component version. - */ - public String getVersion() { return version; } -/** - * This method to return all the fields. - * @return the result in a formatted string. - */ - public String toString() { - String NL = System.getProperty("line.separator"); - StringBuffer buf = new StringBuffer("MBVResult:"+NL); - buf.append("\temail_address = "+email_address+NL); - buf.append("\tdomain = "+domain+NL); - buf.append("\tis_free = "+is_free+NL); - buf.append("\tis_syntax = "+is_syntax+NL); - buf.append("\tis_domain = "+is_domain+NL); - buf.append("\tis_smtp = "+is_smtp+NL); - buf.append("\tis_verified = "+is_verified+NL); - buf.append("\tis_server_down = "+is_server_down+NL); - buf.append("\tis_greylisted = "+is_greylisted+NL); - buf.append("\tis_disposable = "+is_disposable+NL); - buf.append("\tis_suppressed = "+is_suppressed+NL); - buf.append("\tis_role = "+is_role+NL); - buf.append("\tis_high_risk = "+is_high_risk+NL); - buf.append("\tis_catchall = "+is_catchall+NL); - buf.append("\tmailboxvalidator_score = "+mailboxvalidator_score+NL); - buf.append("\ttime_taken = "+time_taken+NL); - buf.append("\tstatus = "+status+NL); - buf.append("\tcredits_available = "+credits_available+NL); - buf.append("\terror_code = "+error_code+NL); - buf.append("\terror_message = "+error_message+NL); - buf.append("\tversion = "+version+NL); - return buf.toString(); - } -} +/** +* MBVResult.java +*/ + +package com.mailboxvalidator; + +public class MBVResult { + String email_address; + String base_email_address; + String domain; + String is_free; + String is_syntax; + String is_domain; + String is_smtp; + String is_verified; + String is_server_down; + String is_greylisted; + String is_disposable; + String is_suppressed; + String is_role; + String is_high_risk; + String is_catchall; + String is_dmarc_enforced; + String is_strict_spf; + String website_exist; + float mailboxvalidator_score; + float time_taken; + String status; + int credits_available; + String error_code; + String error_message; + String version = "2.1.0"; + MBVResult(String email) { + email_address = email; + } +/** + * This method to get the email address being validated. + * @return the email address being validated. + */ + public String getEmailAddress() { return email_address; } +/** + * This method to get the base email address of the email address being validated. + * @return the base email address of the email address being validated. + */ + public String getBaseEmailAddress() { return base_email_address; } +/** + * This method to get the domain name of the email address. + * @return the domain name. + */ + public String getDomain() { return domain; } +/** + * This method to get whether the email address is from a free email provider. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsFree() { return is_free; } +/** + * This method to get whether the email address is syntactically correct. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsSyntax() { return is_syntax; } +/** + * This method to get whether the email domain has a valid MX record in the DNS entries. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsDomain() { return is_domain; } +/** + * This method to get whether the mail server is responding to connection. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsSMTP() { return is_smtp; } +/** + * This method to get whether the mail server confirms that the email address actually exists. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsVerified() { return is_verified; } +/** + * This method to get whether the mail server is currently down or unresponsive. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsServerDown() { return is_server_down; } +/** + * This method to get whether the mail server employs greylisting. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsGreylisted() { return is_greylisted; } +/** + * This method to get whether the email address is from a disposable email provider. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsDisposable() { return is_disposable; } +/** + * This method to get whether the email address is in our blacklist. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsSuppressed() { return is_suppressed; } +/** + * This method to get whether the email address is a role-based email address. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsRole() { return is_role; } +/** + * This method to get whether the email address contains high risk keywords. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsHighRisk() { return is_high_risk; } +/** + * This method to get whether the email address is a catch-all address. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsCatchall() { return is_catchall; } +/** + * This method to get whether the email domain is enforcing DMARC. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsDMARCEnforced() { return is_dmarc_enforced; } +/** + * This method to get whether the email domain is using strict SPF. + * @return "true", "false" or "null" if not applicable. + */ + public String getIsStrictSPF() { return is_strict_spf; } +/** + * This method to get whether the email domain is a reachable website. + * @return "true", "false" or "null" if not applicable. + */ + public String getWebsiteExist() { return website_exist; } +/** + * This method to get the email reputation score. + * @return the email reputation score; score greater than 0.70 means good; score greater than 0.40 means fair; score less than or equal to 0.40 means poor. + */ + public float getMailboxValidatorScore() { return mailboxvalidator_score; } +/** + * This method to get the execution time of the API in seconds. + * @return the time taken for the API execution in seconds. + */ + public float getTimeTaken() { return time_taken; } +/** + * This method to get whether our system think the email address is valid based on the various result fields. + * @return "true", "false" or "null" if not applicable. + */ + public String getStatus() { return status; } +/** + * This method to get the number of remaining query credits. + * @return the number of remaining query credits. + */ + public int getCreditsAvailable() { return credits_available; } +/** + * This method to get the error code. + * @return the error code if an error occurred or blank if none. + */ + public String getErrorCode() { return error_code; } +/** + * This method to get the error message. + * @return the error message if an error occurred or blank if none. + */ + public String getErrorMessage() { return error_message; } +/** + * This method to get component version. + * @return the component version. + */ + public String getVersion() { return version; } +/** + * This method to return all the fields. + * @return the result in a formatted string. + */ + public String toString() { + String NL = System.getProperty("line.separator"); + StringBuffer buf = new StringBuffer("MBVResult:"+NL); + buf.append("\temail_address = "+email_address+NL); + buf.append("\tbase_email_address = "+base_email_address+NL); + buf.append("\tdomain = "+domain+NL); + buf.append("\tis_free = "+is_free+NL); + buf.append("\tis_syntax = "+is_syntax+NL); + buf.append("\tis_domain = "+is_domain+NL); + buf.append("\tis_smtp = "+is_smtp+NL); + buf.append("\tis_verified = "+is_verified+NL); + buf.append("\tis_server_down = "+is_server_down+NL); + buf.append("\tis_greylisted = "+is_greylisted+NL); + buf.append("\tis_disposable = "+is_disposable+NL); + buf.append("\tis_suppressed = "+is_suppressed+NL); + buf.append("\tis_role = "+is_role+NL); + buf.append("\tis_high_risk = "+is_high_risk+NL); + buf.append("\tis_catchall = "+is_catchall+NL); + buf.append("\tis_dmarc_enforced = "+is_dmarc_enforced+NL); + buf.append("\tis_strict_spf = "+is_strict_spf+NL); + buf.append("\twebsite_exist = "+website_exist+NL); + buf.append("\tmailboxvalidator_score = "+mailboxvalidator_score+NL); + buf.append("\ttime_taken = "+time_taken+NL); + buf.append("\tstatus = "+status+NL); + buf.append("\tcredits_available = "+credits_available+NL); + buf.append("\terror_code = "+error_code+NL); + buf.append("\terror_message = "+error_message+NL); + buf.append("\tversion = "+version+NL); + return buf.toString(); + } +} diff --git a/src/com/mailboxvalidator/SingleValidation.java b/src/com/mailboxvalidator/SingleValidation.java index bd71bd0..85435a7 100644 --- a/src/com/mailboxvalidator/SingleValidation.java +++ b/src/com/mailboxvalidator/SingleValidation.java @@ -1,354 +1,366 @@ -package com.mailboxvalidator; - -import java.net.URLEncoder; -import java.util.Map; -import java.util.Hashtable; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; -import java.util.regex.Matcher; -import java.io.BufferedReader; -import java.io.IOException; -import java.net.MalformedURLException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - -public class SingleValidation { - private String api_key = ""; - private static final String api_url = "https://api.mailboxvalidator.com/v2/validation/single"; - private static final String api_url2 = "https://api.mailboxvalidator.com/v2/email/disposable"; - private static final String api_url3 = "https://api.mailboxvalidator.com/v2/email/free"; - private static final Pattern jsonpattern = Pattern.compile("\"([^\"]+)\":(\"[^\"]+\"|[0-9.]+|null|true|false)", Pattern.CASE_INSENSITIVE); - - public SingleValidation(String apikey) { - api_key = apikey; - } - -/** -* This function to validate an email address using the MailboxValidator API -* @param EmailAddress The email address to validate. -* @return MailboxValidator API results -* @throws IOException If API call fails. -*/ - public MBVResult ValidateEmail(String EmailAddress) throws IOException { - MBVResult record = new MBVResult(EmailAddress); - - try { - Hashtable data = new Hashtable(); - data.put("format", "json"); - data.put("email", EmailAddress); - data.put("key", api_key); - - String datastr = ""; - for (Map.Entry entry : data.entrySet()) { - datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); - } - datastr = datastr.substring(1); - URL url = new URL(api_url + "?" + datastr); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - conn.setRequestProperty("Accept", "application/json"); - - if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { - throw new RuntimeException("Error connecting to API."); - } - - String output; - String output2 = ""; - BufferedReader br; - Matcher m; - List keyMatches = new ArrayList(); - List valMatches = new ArrayList(); - if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { - br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); - } - else { - br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); - } - while ((output = br.readLine()) != null) { - output2 = output2 + output; - } - conn.disconnect(); - m = jsonpattern.matcher(output2); - while (m.find()) { - keyMatches.add(m.group(1)); - valMatches.add(m.group(2)); - } - - int x = 0; - int max = keyMatches.size(); - - String v1 = ""; - String v2 = ""; - for (x = 0; x < max; x++) { - v1 = keyMatches.get(x); - v2 = valMatches.get(x); - v2 = v2.replaceAll("^\"", ""); - v2 = v2.replaceAll("\"$", ""); - - switch (v1) { - case "email_address": - record.email_address = v2; - break; - case "domain": - record.domain = v2; - break; - case "is_free": - record.is_free = v2; - break; - case "is_syntax": - record.is_syntax = v2; - break; - case "is_domain": - record.is_domain = v2; - break; - case "is_smtp": - record.is_smtp = v2; - break; - case "is_verified": - record.is_verified = v2; - break; - case "is_server_down": - record.is_server_down = v2; - break; - case "is_greylisted": - record.is_greylisted = v2; - break; - case "is_disposable": - record.is_disposable = v2; - break; - case "is_suppressed": - record.is_suppressed = v2; - break; - case "is_role": - record.is_role = v2; - break; - case "is_high_risk": - record.is_high_risk = v2; - break; - case "is_catchall": - record.is_catchall = v2; - break; - case "mailboxvalidator_score": - record.mailboxvalidator_score = (v2.length() > 0) ? Float.parseFloat(v2) : (float)0.0; - break; - case "time_taken": - record.time_taken = Float.parseFloat(v2); - break; - case "status": - record.status = v2; - break; - case "credits_available": - record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; - break; - case "error_code": - record.error_code = v2; - break; - case "error_message": - record.error_message = v2; - break; - default: - break; - } - } - return record; - } - catch (MalformedURLException ex) { - throw ex; - } - catch (IOException ex) { - throw ex; - } - finally { - } - } - -/** -* This function to check if an email address is from a disposable email provider -* @param EmailAddress The email address to check. -* @return MailboxValidator API results -* @throws IOException If API call fails. -*/ - public MBVResult DisposableEmail(String EmailAddress) throws IOException { - MBVResult record = new MBVResult(EmailAddress); - - try { - Hashtable data = new Hashtable(); - data.put("format", "json"); - data.put("email", EmailAddress); - data.put("key", api_key); - - String datastr = ""; - for (Map.Entry entry : data.entrySet()) { - datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); - } - datastr = datastr.substring(1); - URL url = new URL(api_url2 + "?" + datastr); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - conn.setRequestProperty("Accept", "application/json"); - - if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { - throw new RuntimeException("Error connecting to API."); - } - - String output; - String output2 = ""; - BufferedReader br; - Matcher m; - List keyMatches = new ArrayList(); - List valMatches = new ArrayList(); - if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { - br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); - } - else { - br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); - } - while ((output = br.readLine()) != null) { - output2 = output2 + output; - } - conn.disconnect(); - m = jsonpattern.matcher(output2); - while (m.find()) { - keyMatches.add(m.group(1)); - valMatches.add(m.group(2)); - } - - int x = 0; - int max = keyMatches.size(); - - String v1 = ""; - String v2 = ""; - for (x = 0; x < max; x++) { - v1 = keyMatches.get(x); - v2 = valMatches.get(x); - v2 = v2.replaceAll("^\"", ""); - v2 = v2.replaceAll("\"$", ""); - - switch (v1) { - case "email_address": - record.email_address = v2; - break; - case "is_disposable": - record.is_disposable = v2; - break; - case "credits_available": - record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; - break; - case "error_code": - record.error_code = v2; - break; - case "error_message": - record.error_message = v2; - break; - default: - break; - } - } - return record; - } - catch (MalformedURLException ex) { - throw ex; - } - catch (IOException ex) { - throw ex; - } - finally { - } - } - -/** -* This function to check if an email address is from a free email provider -* @param EmailAddress The email address to check. -* @return MailboxValidator API results -* @throws IOException If API call fails. -*/ - public MBVResult FreeEmail(String EmailAddress) throws IOException { - MBVResult record = new MBVResult(EmailAddress); - - try { - Hashtable data = new Hashtable(); - data.put("format", "json"); - data.put("email", EmailAddress); - data.put("key", api_key); - - String datastr = ""; - for (Map.Entry entry : data.entrySet()) { - datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); - } - datastr = datastr.substring(1); - URL url = new URL(api_url3 + "?" + datastr); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - conn.setRequestProperty("Accept", "application/json"); - - if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { - throw new RuntimeException("Error connecting to API."); - } - - String output; - String output2 = ""; - BufferedReader br; - Matcher m; - List keyMatches = new ArrayList(); - List valMatches = new ArrayList(); - if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { - br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); - } - else { - br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); - } - while ((output = br.readLine()) != null) { - output2 = output2 + output; - } - conn.disconnect(); - m = jsonpattern.matcher(output2); - while (m.find()) { - keyMatches.add(m.group(1)); - valMatches.add(m.group(2)); - } - - int x = 0; - int max = keyMatches.size(); - - String v1 = ""; - String v2 = ""; - for (x = 0; x < max; x++) { - v1 = keyMatches.get(x); - v2 = valMatches.get(x); - v2 = v2.replaceAll("^\"", ""); - v2 = v2.replaceAll("\"$", ""); - - switch (v1) { - case "email_address": - record.email_address = v2; - break; - case "is_free": - record.is_free = v2; - break; - case "credits_available": - record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; - break; - case "error_code": - record.error_code = v2; - break; - case "error_message": - record.error_message = v2; - break; - default: - break; - } - } - return record; - } - catch (MalformedURLException ex) { - throw ex; - } - catch (IOException ex) { - throw ex; - } - finally { - } - } -} +package com.mailboxvalidator; + +import java.net.URLEncoder; +import java.util.Map; +import java.util.Hashtable; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.io.BufferedReader; +import java.io.IOException; +import java.net.MalformedURLException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class SingleValidation { + private String api_key = ""; + private static final String api_url = "https://api.mailboxvalidator.com/v2/validation/single"; + private static final String api_url2 = "https://api.mailboxvalidator.com/v2/email/disposable"; + private static final String api_url3 = "https://api.mailboxvalidator.com/v2/email/free"; + private static final Pattern jsonpattern = Pattern.compile("\"([^\"]+)\":(\"[^\"]+\"|[0-9.]+|null|true|false)", Pattern.CASE_INSENSITIVE); + + public SingleValidation(String apikey) { + api_key = apikey; + } + +/** +* This function to validate an email address using the MailboxValidator API +* @param EmailAddress The email address to validate. +* @return MailboxValidator API results +* @throws IOException If API call fails. +*/ + public MBVResult ValidateEmail(String EmailAddress) throws IOException { + MBVResult record = new MBVResult(EmailAddress); + + try { + Hashtable data = new Hashtable(); + data.put("format", "json"); + data.put("email", EmailAddress); + data.put("key", api_key); + + String datastr = ""; + for (Map.Entry entry : data.entrySet()) { + datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); + } + datastr = datastr.substring(1); + URL url = new URL(api_url + "?" + datastr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Accept", "application/json"); + + if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { + throw new RuntimeException("Error connecting to API."); + } + + String output; + String output2 = ""; + BufferedReader br; + Matcher m; + List keyMatches = new ArrayList(); + List valMatches = new ArrayList(); + if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { + br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); + } + else { + br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); + } + while ((output = br.readLine()) != null) { + output2 = output2 + output; + } + conn.disconnect(); + m = jsonpattern.matcher(output2); + while (m.find()) { + keyMatches.add(m.group(1)); + valMatches.add(m.group(2)); + } + + int x = 0; + int max = keyMatches.size(); + + String v1 = ""; + String v2 = ""; + for (x = 0; x < max; x++) { + v1 = keyMatches.get(x); + v2 = valMatches.get(x); + v2 = v2.replaceAll("^\"", ""); + v2 = v2.replaceAll("\"$", ""); + + switch (v1) { + case "email_address": + record.email_address = v2; + break; + case "base_email_address": + record.base_email_address = v2; + break; + case "domain": + record.domain = v2; + break; + case "is_free": + record.is_free = v2; + break; + case "is_syntax": + record.is_syntax = v2; + break; + case "is_domain": + record.is_domain = v2; + break; + case "is_smtp": + record.is_smtp = v2; + break; + case "is_verified": + record.is_verified = v2; + break; + case "is_server_down": + record.is_server_down = v2; + break; + case "is_greylisted": + record.is_greylisted = v2; + break; + case "is_disposable": + record.is_disposable = v2; + break; + case "is_suppressed": + record.is_suppressed = v2; + break; + case "is_role": + record.is_role = v2; + break; + case "is_high_risk": + record.is_high_risk = v2; + break; + case "is_catchall": + record.is_catchall = v2; + break; + case "is_dmarc_enforced": + record.is_dmarc_enforced = v2; + break; + case "is_strict_spf": + record.is_strict_spf = v2; + break; + case "website_exist": + record.website_exist = v2; + break; + case "mailboxvalidator_score": + record.mailboxvalidator_score = (v2.length() > 0) ? Float.parseFloat(v2) : (float)0.0; + break; + case "time_taken": + record.time_taken = Float.parseFloat(v2); + break; + case "status": + record.status = v2; + break; + case "credits_available": + record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; + break; + case "error_code": + record.error_code = v2; + break; + case "error_message": + record.error_message = v2; + break; + default: + break; + } + } + return record; + } + catch (MalformedURLException ex) { + throw ex; + } + catch (IOException ex) { + throw ex; + } + finally { + } + } + +/** +* This function to check if an email address is from a disposable email provider +* @param EmailAddress The email address to check. +* @return MailboxValidator API results +* @throws IOException If API call fails. +*/ + public MBVResult DisposableEmail(String EmailAddress) throws IOException { + MBVResult record = new MBVResult(EmailAddress); + + try { + Hashtable data = new Hashtable(); + data.put("format", "json"); + data.put("email", EmailAddress); + data.put("key", api_key); + + String datastr = ""; + for (Map.Entry entry : data.entrySet()) { + datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); + } + datastr = datastr.substring(1); + URL url = new URL(api_url2 + "?" + datastr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Accept", "application/json"); + + if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { + throw new RuntimeException("Error connecting to API."); + } + + String output; + String output2 = ""; + BufferedReader br; + Matcher m; + List keyMatches = new ArrayList(); + List valMatches = new ArrayList(); + if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { + br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); + } + else { + br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); + } + while ((output = br.readLine()) != null) { + output2 = output2 + output; + } + conn.disconnect(); + m = jsonpattern.matcher(output2); + while (m.find()) { + keyMatches.add(m.group(1)); + valMatches.add(m.group(2)); + } + + int x = 0; + int max = keyMatches.size(); + + String v1 = ""; + String v2 = ""; + for (x = 0; x < max; x++) { + v1 = keyMatches.get(x); + v2 = valMatches.get(x); + v2 = v2.replaceAll("^\"", ""); + v2 = v2.replaceAll("\"$", ""); + + switch (v1) { + case "email_address": + record.email_address = v2; + break; + case "is_disposable": + record.is_disposable = v2; + break; + case "credits_available": + record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; + break; + case "error_code": + record.error_code = v2; + break; + case "error_message": + record.error_message = v2; + break; + default: + break; + } + } + return record; + } + catch (MalformedURLException ex) { + throw ex; + } + catch (IOException ex) { + throw ex; + } + finally { + } + } + +/** +* This function to check if an email address is from a free email provider +* @param EmailAddress The email address to check. +* @return MailboxValidator API results +* @throws IOException If API call fails. +*/ + public MBVResult FreeEmail(String EmailAddress) throws IOException { + MBVResult record = new MBVResult(EmailAddress); + + try { + Hashtable data = new Hashtable(); + data.put("format", "json"); + data.put("email", EmailAddress); + data.put("key", api_key); + + String datastr = ""; + for (Map.Entry entry : data.entrySet()) { + datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); + } + datastr = datastr.substring(1); + URL url = new URL(api_url3 + "?" + datastr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Accept", "application/json"); + + if ((conn.getResponseCode() != 200) && (conn.getResponseCode() != 400) && (conn.getResponseCode() != 401)) { + throw new RuntimeException("Error connecting to API."); + } + + String output; + String output2 = ""; + BufferedReader br; + Matcher m; + List keyMatches = new ArrayList(); + List valMatches = new ArrayList(); + if ((conn.getResponseCode() == 400) || (conn.getResponseCode() == 401)) { + br = new BufferedReader(new InputStreamReader((conn.getErrorStream()))); + } + else { + br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); + } + while ((output = br.readLine()) != null) { + output2 = output2 + output; + } + conn.disconnect(); + m = jsonpattern.matcher(output2); + while (m.find()) { + keyMatches.add(m.group(1)); + valMatches.add(m.group(2)); + } + + int x = 0; + int max = keyMatches.size(); + + String v1 = ""; + String v2 = ""; + for (x = 0; x < max; x++) { + v1 = keyMatches.get(x); + v2 = valMatches.get(x); + v2 = v2.replaceAll("^\"", ""); + v2 = v2.replaceAll("\"$", ""); + + switch (v1) { + case "email_address": + record.email_address = v2; + break; + case "is_free": + record.is_free = v2; + break; + case "credits_available": + record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; + break; + case "error_code": + record.error_code = v2; + break; + case "error_message": + record.error_message = v2; + break; + default: + break; + } + } + return record; + } + catch (MalformedURLException ex) { + throw ex; + } + catch (IOException ex) { + throw ex; + } + finally { + } + } +}