Skip to content

Commit 3577236

Browse files
Added disposable & free email APIs
1 parent 4941e89 commit 3577236

File tree

3 files changed

+342
-4
lines changed

3 files changed

+342
-4
lines changed

README.md

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Dependencies
2424

2525
An API key is required for this class to function.
2626

27-
Go to https://www.mailboxvalidator.com/plans#api to sign up for a FREE API plan and you'll be given an API key.
27+
Go to http://www.mailboxvalidator.com/plans#api to sign up for a FREE API plan and you'll be given an API key.
2828

29-
Usage
30-
=====
29+
Usage for validating emails
30+
===========================
3131

3232
```java
3333
import com.mailboxvalidator.*;
@@ -202,6 +202,160 @@ The error code if there is any error. See error table below.
202202

203203
The error message if there is any error. See error table below.
204204

205+
206+
Usage for checking if an email is from a disposable email provider
207+
==================================================================
208+
209+
```java
210+
import com.mailboxvalidator.*;
211+
212+
public class Main
213+
{
214+
public Main()
215+
{
216+
}
217+
218+
public static void main(String[] args)
219+
{
220+
try
221+
{
222+
SingleValidation mbv = new SingleValidation("PASTE_YOUR_API_KEY_HERE");
223+
MBVResult rec = mbv.DisposableEmail("[email protected]");
224+
// System.out.println(rec); // for dumping out all result fields
225+
226+
if (rec.getErrorMessage().equals("")) {
227+
System.out.println("email_address: " + rec.getEmailAddress());
228+
System.out.println("is_disposable: " + rec.getIsDisposable());
229+
System.out.println("credits_available: " + rec.getCreditsAvailable());
230+
}
231+
else {
232+
System.out.println("error_code: " + rec.getErrorCode());
233+
System.out.println("error_message: " + rec.getErrorMessage());
234+
}
235+
System.out.println("version: " + rec.getVersion());
236+
}
237+
catch(Exception e)
238+
{
239+
e.printStackTrace(System.out);
240+
}
241+
}
242+
}
243+
```
244+
245+
Functions
246+
=========
247+
248+
### SingleValidation(api_key)
249+
250+
Creates a new instance of the MailboxValidator object with the API key.
251+
252+
### DisposableEmail(email_address)
253+
254+
Check if the supplied email address is from a disposable email provider.
255+
256+
Result Fields
257+
=============
258+
259+
### email_address
260+
261+
The input email address.
262+
263+
### is_disposable
264+
265+
Whether the email address is a temporary one from a disposable email provider.
266+
267+
Return values: True, False
268+
269+
### credits_available
270+
271+
The number of credits left to perform validations.
272+
273+
### error_code
274+
275+
The error code if there is any error. See error table below.
276+
277+
### error_message
278+
279+
The error message if there is any error. See error table below.
280+
281+
282+
Usage for checking if an email is from a free email provider
283+
============================================================
284+
285+
```java
286+
import com.mailboxvalidator.*;
287+
288+
public class Main
289+
{
290+
public Main()
291+
{
292+
}
293+
294+
public static void main(String[] args)
295+
{
296+
try
297+
{
298+
SingleValidation mbv = new SingleValidation("PASTE_YOUR_API_KEY_HERE");
299+
MBVResult rec = mbv.FreeEmail("[email protected]");
300+
// System.out.println(rec); // for dumping out all result fields
301+
302+
if (rec.getErrorMessage().equals("")) {
303+
System.out.println("email_address: " + rec.getEmailAddress());
304+
System.out.println("is_free: " + rec.getIsFree());
305+
System.out.println("credits_available: " + rec.getCreditsAvailable());
306+
}
307+
else {
308+
System.out.println("error_code: " + rec.getErrorCode());
309+
System.out.println("error_message: " + rec.getErrorMessage());
310+
}
311+
System.out.println("version: " + rec.getVersion());
312+
}
313+
catch(Exception e)
314+
{
315+
e.printStackTrace(System.out);
316+
}
317+
}
318+
}
319+
```
320+
321+
Functions
322+
=========
323+
324+
### SingleValidation(api_key)
325+
326+
Creates a new instance of the MailboxValidator object with the API key.
327+
328+
### FreeEmail(email_address)
329+
330+
Check if the supplied email address is from a free email provider.
331+
332+
Result Fields
333+
=============
334+
335+
### email_address
336+
337+
The input email address.
338+
339+
### is_free
340+
341+
Whether the email address is from a free email provider like Gmail or Hotmail.
342+
343+
Return values: True, False
344+
345+
### credits_available
346+
347+
The number of credits left to perform validations.
348+
349+
### error_code
350+
351+
The error code if there is any error. See error table below.
352+
353+
### error_message
354+
355+
The error message if there is any error. See error table below.
356+
357+
358+
205359
Errors
206360
======
207361

src/com/mailboxvalidator/MBVResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MBVResult {
2525
int credits_available;
2626
String error_code;
2727
String error_message;
28-
String version = "1.0.0";
28+
String version = "1.1.0";
2929
MBVResult(String email) {
3030
email_address = email;
3131
}

src/com/mailboxvalidator/SingleValidation.java

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
public class SingleValidation {
1818
private String api_key = "";
1919
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";
2022
private static final Pattern jsonpattern = Pattern.compile("(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|[0-9\\.\\-]+)", Pattern.CASE_INSENSITIVE);
2123

2224
public SingleValidation(String apikey) {
@@ -158,4 +160,186 @@ public MBVResult ValidateEmail(String EmailAddress) throws IOException {
158160
finally {
159161
}
160162
}
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+
}
161345
}

0 commit comments

Comments
 (0)