EMR 内没有 smtp 提供程序

No provider for smtp inside EMR

我正在尝试在 EMR 作业完成时发送电子邮件。我在我的 sbt 依赖项中使用了 libraryDependencies += "com.sun.mail" % "javax.mail" % "1.6.2".

我测试了在本地发送的电子邮件,它工作正常,但同样的工作在 EMR 作业中失败,但出现以下异常:

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.run(ApplicationMaster.scala:706)

使用的代码是` public class EmailUtil { public static void sendEmail(Session session, String toEmail, String subject, String body){ 尝试 { MimeMessage 消息 = new MimeMessage(session); //设置消息headers msg.addHeader("Content-type", "text/HTML; 字符集=UTF-8"); msg.addHeader("格式", "流动"); 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");
}

}`

该应用程序是使用 SBT 构建的,并使用了以下合并策略。

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

创建的 fat jar 没有来自 javax.mail jar 的必要 META_INF 文件,这导致了一个问题。出于测试目的,我手动将 META-INF 文件从 javax.mail jar 复制到 fat jar 并且它起作用了。