You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

package hs.fulda.de.ci.exam.project;
import java.util.regex.Pattern;
public class EmailNotification extends Notification {
String email;
String content;
public EmailNotification(String email, String content) {
super();
this.email = email;
this.content = content;
}
@Override
public boolean sendNotification() {
if (isValidEmail(email)) {
System.out.println("Email is sent to " + email);
return true;
}
System.out.println("Invalid Email Address");
return false;
}
private boolean isValidEmail(String email) {
String regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@"
+ "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
return patternMatches(email, regexPattern);
}
public static boolean patternMatches(String emailAddress, String regexPattern) {
return Pattern.compile(regexPattern)
.matcher(emailAddress)
.matches();
}
}