java邮件api的NoSuchProviderException

时间:2013-12-22 20:47:08

标签: java maven javamail

我在eclipse中创建了一个简单的maven项目,以便通过一个个人Gmail帐户发送一些电子邮件。在eclipse中,邮件已成功发送。然后我将项目构建到具有依赖项的可执行jar中。但是,当我从命令行运行jar时,它会抛出以下异常:

Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: smtp
    at EmailSender.send(EmailSender.java:75)
    at EmailSender.createNewAccount(EmailSender.java:33)
    at EmailSender.main(EmailSender.java:18)
Caused by: javax.mail.NoSuchProviderException: smtp
    at javax.mail.Session.getService(Session.java:792)
    at javax.mail.Session.getTransport(Session.java:728)
    at javax.mail.Session.getTransport(Session.java:668)
    at javax.mail.Session.getTransport(Session.java:648)
    at javax.mail.Session.getTransport(Session.java:705)
    at javax.mail.Transport.send0(Transport.java:248)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailSender.send(EmailSender.java:73)
    ... 2 more

有谁知道在Eclipse中执行程序和外面的jar之间的区别是什么?非常感谢!

2 个答案:

答案 0 :(得分:0)

你的META-INF文件夹中应该有一个包含所有提供者文件的服务文件夹,你也应该将它们导出到jar中。 eclipse默认包含它们,但导出的jar不是

答案 1 :(得分:0)

我通过修改pom.xml解决了这个问题,因此它可以导出所有依赖项。诀窍是删除行

<minimizeJar>true</minimizeJar>

在以下代码中

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>*******</groupId>
    <artifactId>**********</artifactId>
    <version>1</version>
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/shaded</outputDirectory>
                            <finalName>d2si-mailsender</finalName>
                            <minimizeJar>true</minimizeJar>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>EmailSender</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>