|
17 | 17 | public class SingleValidation { |
18 | 18 | private String api_key = ""; |
19 | 19 | private static final String api_url = "http://api.mailboxvalidator.com/v1/validation/single"; |
| 20 | + private static final String api_url2 = "http://api.mailboxvalidator.com/v1/email/disposable"; |
| 21 | + private static final String api_url3 = "http://api.mailboxvalidator.com/v1/email/free"; |
20 | 22 | private static final Pattern jsonpattern = Pattern.compile("(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|[0-9\\.\\-]+)", Pattern.CASE_INSENSITIVE); |
21 | 23 |
|
22 | 24 | public SingleValidation(String apikey) { |
@@ -158,4 +160,186 @@ public MBVResult ValidateEmail(String EmailAddress) throws IOException { |
158 | 160 | finally { |
159 | 161 | } |
160 | 162 | } |
| 163 | + |
| 164 | +/** |
| 165 | +* This function to check if an email address is from a disposable email provider |
| 166 | +* @param EmailAddress The email address to check. |
| 167 | +* @return MailboxValidator API results |
| 168 | +*/ |
| 169 | + public MBVResult DisposableEmail(String EmailAddress) throws IOException { |
| 170 | + MBVResult record = new MBVResult(EmailAddress); |
| 171 | + |
| 172 | + try { |
| 173 | + Hashtable<String, String> data = new Hashtable<String, String>(); |
| 174 | + data.put("format", "json"); |
| 175 | + data.put("email", EmailAddress); |
| 176 | + data.put("key", api_key); |
| 177 | + |
| 178 | + String datastr = ""; |
| 179 | + for (Map.Entry<String,String> entry : data.entrySet()) { |
| 180 | + datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); |
| 181 | + } |
| 182 | + datastr = datastr.substring(1); |
| 183 | + URL url = new URL(api_url2 + "?" + datastr); |
| 184 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 185 | + conn.setRequestMethod("GET"); |
| 186 | + conn.setRequestProperty("Accept", "application/json"); |
| 187 | + |
| 188 | + if (conn.getResponseCode() != 200) { |
| 189 | + throw new RuntimeException("Error connecting to API."); |
| 190 | + } |
| 191 | + |
| 192 | + BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
| 193 | + |
| 194 | + String output; |
| 195 | + String output2 = ""; |
| 196 | + while ((output = br.readLine()) != null) { |
| 197 | + output2 = output2 + output; |
| 198 | + } |
| 199 | + conn.disconnect(); |
| 200 | + |
| 201 | + List<String> allMatches = new ArrayList<String>(); |
| 202 | + Matcher m = jsonpattern.matcher(output2); |
| 203 | + while (m.find()) { |
| 204 | + allMatches.add(m.group()); |
| 205 | + } |
| 206 | + |
| 207 | + int x = 0; |
| 208 | + int max = allMatches.size(); |
| 209 | + |
| 210 | + String v1 = ""; |
| 211 | + String v2 = ""; |
| 212 | + for (x = 0; x < max; x = x + 2) { |
| 213 | + v1 = allMatches.get(x).substring(1); |
| 214 | + v1 = v1.substring(0, v1.length() - 1); |
| 215 | + v2 = allMatches.get(x + 1); |
| 216 | + |
| 217 | + if (!v1.equals("credits_available")) { |
| 218 | + v2 = v2.substring(1); |
| 219 | + v2 = v2.substring(0, v2.length() - 1); |
| 220 | + } |
| 221 | + |
| 222 | + switch (v1) { |
| 223 | + case "email_address": |
| 224 | + record.email_address = v2; |
| 225 | + break; |
| 226 | + case "is_disposable": |
| 227 | + record.is_disposable = v2; |
| 228 | + break; |
| 229 | + case "credits_available": |
| 230 | + v2 = v2.replaceAll("\"", ""); |
| 231 | + record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; |
| 232 | + break; |
| 233 | + case "error_code": |
| 234 | + record.error_code = v2; |
| 235 | + break; |
| 236 | + case "error_message": |
| 237 | + record.error_message = v2; |
| 238 | + break; |
| 239 | + default: |
| 240 | + break; |
| 241 | + } |
| 242 | + } |
| 243 | + return record; |
| 244 | + } |
| 245 | + catch (MalformedURLException ex) { |
| 246 | + throw ex; |
| 247 | + } |
| 248 | + catch (IOException ex) { |
| 249 | + throw ex; |
| 250 | + } |
| 251 | + finally { |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | +/** |
| 256 | +* This function to check if an email address is from a free email provider |
| 257 | +* @param EmailAddress The email address to check. |
| 258 | +* @return MailboxValidator API results |
| 259 | +*/ |
| 260 | + public MBVResult FreeEmail(String EmailAddress) throws IOException { |
| 261 | + MBVResult record = new MBVResult(EmailAddress); |
| 262 | + |
| 263 | + try { |
| 264 | + Hashtable<String, String> data = new Hashtable<String, String>(); |
| 265 | + data.put("format", "json"); |
| 266 | + data.put("email", EmailAddress); |
| 267 | + data.put("key", api_key); |
| 268 | + |
| 269 | + String datastr = ""; |
| 270 | + for (Map.Entry<String,String> entry : data.entrySet()) { |
| 271 | + datastr += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); |
| 272 | + } |
| 273 | + datastr = datastr.substring(1); |
| 274 | + URL url = new URL(api_url3 + "?" + datastr); |
| 275 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 276 | + conn.setRequestMethod("GET"); |
| 277 | + conn.setRequestProperty("Accept", "application/json"); |
| 278 | + |
| 279 | + if (conn.getResponseCode() != 200) { |
| 280 | + throw new RuntimeException("Error connecting to API."); |
| 281 | + } |
| 282 | + |
| 283 | + BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
| 284 | + |
| 285 | + String output; |
| 286 | + String output2 = ""; |
| 287 | + while ((output = br.readLine()) != null) { |
| 288 | + output2 = output2 + output; |
| 289 | + } |
| 290 | + conn.disconnect(); |
| 291 | + |
| 292 | + List<String> allMatches = new ArrayList<String>(); |
| 293 | + Matcher m = jsonpattern.matcher(output2); |
| 294 | + while (m.find()) { |
| 295 | + allMatches.add(m.group()); |
| 296 | + } |
| 297 | + |
| 298 | + int x = 0; |
| 299 | + int max = allMatches.size(); |
| 300 | + |
| 301 | + String v1 = ""; |
| 302 | + String v2 = ""; |
| 303 | + for (x = 0; x < max; x = x + 2) { |
| 304 | + v1 = allMatches.get(x).substring(1); |
| 305 | + v1 = v1.substring(0, v1.length() - 1); |
| 306 | + v2 = allMatches.get(x + 1); |
| 307 | + |
| 308 | + if (!v1.equals("credits_available")) { |
| 309 | + v2 = v2.substring(1); |
| 310 | + v2 = v2.substring(0, v2.length() - 1); |
| 311 | + } |
| 312 | + |
| 313 | + switch (v1) { |
| 314 | + case "email_address": |
| 315 | + record.email_address = v2; |
| 316 | + break; |
| 317 | + case "is_free": |
| 318 | + record.is_free = v2; |
| 319 | + break; |
| 320 | + case "credits_available": |
| 321 | + v2 = v2.replaceAll("\"", ""); |
| 322 | + record.credits_available = (v2.length() > 0) ? Integer.parseInt(v2) : 0; |
| 323 | + break; |
| 324 | + case "error_code": |
| 325 | + record.error_code = v2; |
| 326 | + break; |
| 327 | + case "error_message": |
| 328 | + record.error_message = v2; |
| 329 | + break; |
| 330 | + default: |
| 331 | + break; |
| 332 | + } |
| 333 | + } |
| 334 | + return record; |
| 335 | + } |
| 336 | + catch (MalformedURLException ex) { |
| 337 | + throw ex; |
| 338 | + } |
| 339 | + catch (IOException ex) { |
| 340 | + throw ex; |
| 341 | + } |
| 342 | + finally { |
| 343 | + } |
| 344 | + } |
161 | 345 | } |
0 commit comments