* @return 0 for success, -1 for failure
* @throws GenieException on issue
*/
private boolean sendEmail(String emailTo, boolean killed) throws GenieException {
LOG.debug("called");
final Job job = this.jobService.getJob(this.jobId);
if (!this.config.getBoolean("netflix.genie.server.mail.enable", false)) {
LOG.warn("Email is disabled but user has specified an email address.");
return false;
}
// Sender's email ID
String fromEmail = this.config.getString("netflix.genie.server.mail.smpt.from", "no-reply-genie@geniehost.com");
LOG.info("From email address to use to send email: "
+ fromEmail);
// Set the smtp server hostname. Use localhost as default
String smtpHost = this.config.getString("netflix.genie.server.mail.smtp.host", "localhost");
LOG.debug("Email smtp server: "
+ smtpHost);
// Get system properties
Properties properties = new Properties();
// Setup mail server
properties.setProperty("mail.smtp.host", smtpHost);
// check whether authentication should be turned on
Authenticator auth = null;
if (this.config.getBoolean("netflix.genie.server.mail.smtp.auth", false)) {
LOG.debug("Email Authentication Enabled");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
String userName = config.getString("netflix.genie.server.mail.smtp.user");
String password = config.getString("netflix.genie.server.mail.smtp.password");
if ((userName == null) || (password == null)) {
LOG.error("Authentication is enabled and username/password for smtp server is null");
return false;
}
LOG.debug("Constructing authenticator object with username"
+ userName
+ " and password "
+ password);
auth = new SMTPAuthenticator(userName,
password);
} else {
LOG.debug("Email authentication not enabled.");
}
// Get the default Session object.
Session session = Session.getInstance(properties, auth);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emailTo));
JobStatus jobStatus;
if (killed) {
jobStatus = JobStatus.KILLED;
} else {
jobStatus = job.getStatus();
}
// Set Subject: header field
message.setSubject("Genie Job "
+ job.getName()
+ " completed with Status: "
+ jobStatus);
// Now set the actual message
String body = "Your Genie Job is complete\n\n"
+ "Job ID: "
+ job.getId()
+ "\n"
+ "Job Name: "
+ job.getName()
+ "\n"
+ "Status: "
+ job.getStatus()
+ "\n"
+ "Status Message: "
+ job.getStatusMsg()
+ "\n"
+ "Output Base URL: "
+ job.getOutputURI()
+ "\n";
message.setText(body);
// Send message