Java Mail Issue with Session.getInstance

删除回忆录丶 提交于 2019-11-28 09:50:21

Try adding this into Maven POM:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.2</version>
</dependency>

The problem is that you are trying to use javax.mail-api.jar. That is the wrong library. JavaMail is a Java EE specification, the interfaces of that specification are published in that javax.mail-api-1.6.1.jar, which only works when compiling. It doesn't provide an implementation of the specification, so it doesn't work at runtime.

You need to use an implementation of the JavaMail specification at runtime. You can find the reference implementation on https://javaee.github.io/javamail/ (but there are others, for example Java EE application servers usually include one).

For javax.mail-api.jar, https://javaee.github.io/javamail/ says:

The JavaMail API definitions only, suitable for compiling against; use only with a Maven “provided” dependency scope

Specifically in your case you need either javax.mail.jar or mailapi.jar + the jars of the specific protocols you want to use. For example mailapi.jar + smtp.jar if you only need smtp(s) support.

With Maven, you can use

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.1</version>
</dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!