Avatar

Level 1

I wrote an External Authorization handler and have added it to one of my policies. The process of creating it and deploying it all seemed to work successfully. However, when I protect a document using the policy, something is not working correctly. My external authorization handler is supposed to send an email once it gets invoked. However, when I open the document, no email is sent. Can someone help me? Here is my code:

public ExternalAuthResultDTO evaluate(ExternalAuthDTO arg0) {
ExternalAuthResultDTO retObj = new ExternalAuthResultDTO();
String docId = arg0.getAlternateId();
String clientIP = arg0.getClientIPAddress();
ArrayList<?> permissions = arg0.getPermissions();
retObj.setPermissions(permissions);

String host = "smtp.gmail.com";
String from = "username";
String pass = "password";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

String[] to = {"user@gmail.com"};

Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) {
    toAddress[i] = new InternetAddress(to[i]);
}
for( int i=0; i < toAddress.length; i++) {
    message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("Document Opened");
message.setText("Document: " + docId + "\nClient IP: " + clientIP);
  
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return retObj;
}