I'm trying to send an email on completion of the EMR job. I have used libraryDependencies += "com.sun.mail" % "javax.mail" % "1.6.2" in my sbt dependencies.
I tested the email sending in local and it works fine but the same job fails in the EMR job with the below exception:
20/11/21 04:28:12 WARN mail: expected resource not found: /META-INF/javamail.default.address.map
javax.mail.NoSuchProviderException: No provider for smtp
at javax.mail.Session.getProvider(Session.java:545)
at javax.mail.Session.getTransport(Session.java:744)
at javax.mail.Session.getTransport(Session.java:725)
at javax.mail.Session.getTransport(Session.java:782)
at javax.mail.Transport.send0(Transport.java:249)
at javax.mail.Transport.send(Transport.java:124)
at util.EmailUtil.sendEmail(EmailUtil.java:44)
at util.EmailUtil.sendMail(EmailUtil.java:59)
at com.here.places.rule.testing.EmailTester$.main(EmailTester.scala:16)
at com.here.places.rule.testing.EmailTester.main(EmailTester.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$4.run(ApplicationMaster.scala:706)
The code used is ` public class EmailUtil { public static void sendEmail(Session session, String toEmail, String subject, String body){ try { MimeMessage msg = new MimeMessage(session); //set message headers msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); msg.addHeader("format", "flowed"); msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(toEmail));
msg.setReplyTo(InternetAddress.parse(toEmail));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void sendMail() {
Properties props = System.getProperties();
props.put("mail.smtp.host", "***");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, null);
sendEmail(session, "***","SimpleEmail Testing Subject", "SimpleEmail Testing Body");
}
}`