*/
public class MailUtils {
public static boolean validate(Mail email) throws MailException {
if (email.getText() == null && email.getHtml() == null) {
throw new MailException(MailException.MISSING_CONTENT);
} else if (email.getSubject() == null || email.getSubject().equals("")) {
throw new MailException(MailException.MISSING_SUBJECT);
} else if (email.getTo().isEmpty() && email.getBcc().isEmpty()
&& email.getCc().isEmpty()) {
throw new MailException(MailException.MISSING_RECIPIENT);
} else if (email.getFrom() == null) {
throw new MailException(MailException.MISSING_SENDER);
} else {
String exceptionMessage = null;
try {
exceptionMessage = MailException.INVALID_SENDER;
email.getFrom().validate();
if (email.getReplyTo() != null) {
exceptionMessage = MailException.INVALID_REPLYTO;
email.getReplyTo().validate();
}
exceptionMessage = MailException.INVALID_TO;
for (InternetAddress a : email.getTo()) {
a.validate();
}
exceptionMessage = MailException.INVALID_BCC;
for (InternetAddress a : email.getBcc()) {
a.validate();
}
exceptionMessage = MailException.INVALID_CC;
for (InternetAddress a : email.getCc()) {
a.validate();
}
} catch (AddressException ex) {
throw new MailException(String.format(exceptionMessage, email),
ex);
}
}
return true;