mirror of
https://github.com/disposable-email-domains/disposable-email-domains.git
synced 2024-11-24 09:38:13 +08:00
Add Java example (#439)
* Added Java example * fix typo --------- Co-authored-by: Martin Cech <emulatorer@gmail.com>
This commit is contained in:
parent
78adac30e7
commit
cdbf59f09b
39
README.md
39
README.md
@ -175,3 +175,42 @@ fi
|
|||||||
echo "$return_value"
|
echo "$return_value"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Java
|
||||||
|
|
||||||
|
Code assumes that you have added `disposable_email_blocklist.conf` next to your class as classpath resource.
|
||||||
|
|
||||||
|
```Java
|
||||||
|
private static final Set<String> DISPOSABLE_EMAIL_DOMAINS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
Set<String> domains = new HashSet<>();
|
||||||
|
try (BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(
|
||||||
|
EMailChecker.class.getResourceAsStream("disposable_email_blocklist.conf"), StandardCharsets.UTF_8))) {
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
line = line.trim();
|
||||||
|
if (line.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
domains.add(line);
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LOG.error("Failed to load list of disposable email domains.", ex);
|
||||||
|
}
|
||||||
|
DISPOSABLE_EMAIL_DOMAINS = domains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isDisposable(String email) throws AddressException {
|
||||||
|
InternetAddress contact = new InternetAddress(email);
|
||||||
|
return isDisposable(contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isDisposable(InternetAddress contact) throws AddressException {
|
||||||
|
String address = contact.getAddress();
|
||||||
|
int domainSep = address.indexOf('@');
|
||||||
|
String domain = (domainSep >= 0) ? address.substring(domainSep + 1) : address;
|
||||||
|
return DISPOSABLE_EMAIL_DOMAINS.contains(domain);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user