java javax.mail.NoSuchProviderException: smtp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26258348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.mail.NoSuchProviderException: smtp
提问by amit
Following is my code to send an email:
以下是我发送电子邮件的代码:
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "false");
final Session session = Session.getInstance(props);
final Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);
msg.setSentDate(new Date());
Multipart multipart = new MimeMultipart("related");
BodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(body, "text/html");
multipart.addBodyPart(mbp1);
Transport.send(msg);
Error Stack trace:
错误堆栈跟踪:
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:764)
at javax.mail.Session.getTransport(Session.java:689)
at javax.mail.Session.getTransport(Session.java:632)
at javax.mail.Session.getTransport(Session.java:612)
at javax.mail.Session.getTransport(Session.java:667)
at javax.mail.Transport.send0(Transport.java:154)
at javax.mail.Transport.send(Transport.java:80)
Note:
笔记:
- Same code works if executed as a desktop application. But throws above exception when deployed on tomcat.
- Latest mail.jar and smtp.jar are added to library.
- SMTP host address is also correct.
- 如果作为桌面应用程序执行,相同的代码也能工作。但是在tomcat上部署时抛出上述异常。
- 最新的 mail.jar 和 smtp.jar 被添加到库中。
- SMTP 主机地址也正确。
If someone can give me pointers it will be helpful.
如果有人可以给我指点,那将是有帮助的。
回答by user1928058
I also got into similar situation, but eventually could solve it. In my case issue was the proguard_rules.txt
file where I needed to add:
我也遇到了类似的情况,但最终可以解决它。就我而言,问题是proguard_rules.txt
我需要添加的文件:
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}
-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
回答by Sumit Sundriyal
I had mail.jar and activation.jar in tomcat's lib directory and mailapi.jar in application's lib directory. Application was reading mailapi.jar during runtime, since mailapi.jar is light weight version of mailing api and it needs smtp.jar that why application was throwing smtp exception. So, if you want to get rid of this exception,
我在tomcat的lib目录中有mail.jar和activation.jar,在应用程序的lib目录中有mailapi.jar。应用程序在运行时正在读取 mailapi.jar,因为 mailapi.jar 是邮件 API 的轻量级版本,它需要 smtp.jar,这就是应用程序抛出 smtp 异常的原因。所以,如果你想摆脱这个异常,
Please resolve conflict between mail.jar and mailapi.jar by:
请通过以下方式解决 mail.jar 和 mailapi.jar 之间的冲突:
- removing mailapi.jar (if it is there in the classpath)
- OR just keep one pair of mailapi.jar and smtp.jar in the classpath and remove mail.jar
- OR just keep one pair of mail.jar and activation.jar in the classpath (clean approach)
- 删除 mailapi.jar(如果它在类路径中)
- 或者只在类路径中保留一对 mailapi.jar 和 smtp.jar 并删除 mail.jar
- 或者只在类路径中保留一对 mail.jar 和 activation.jar(干净的方法)
(FYI:I searched file system to find out mail related jar files and got to know that I have conflicting jar file in the following path (added by gradle in classpath) C:\workspace\.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testapp\WEB-INF\lib)
(仅供参考:我搜索了文件系统以找出与邮件相关的 jar 文件,并知道我在以下路径中有冲突的 jar 文件(由类路径中的 gradle 添加)C:\workspace\.metadata.plugins\org.eclipse.wst .server.core\tmp0\wtpwebapps\testapp\WEB-INF\lib)
回答by Thomas Sunderland
I encountered this issue in an Android project that I work on after introducing AWS SNS into the project. It turned out that the aws-android-sdk-core-2.2.20.jarlibrary includes a file called javamail.providersin its META-INF folder.
我在将 AWS SNS 引入项目后,在我从事的一个 Android 项目中遇到了这个问题。结果证明aws-android-sdk-core-2.2.20.jar库在其 META-INF 文件夹中包含一个名为javamail.providers 的文件。
Once I removed this file from the .jar this exception went away and I was able to send email via java mail once again.
一旦我从 .jar 中删除了这个文件,这个异常就消失了,我能够再次通过 java 邮件发送电子邮件。