Java 邮件:没有 smtp 提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2980408/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:12:08  来源:igfitidea点击:

Java Mail : No provider for smtp

javaemailjavamail

提问by pablof

I am using JavaMail to do a simple application that sends an email when it finds some files in a directory. I managed to get it worked from Eclipse. I Run the application and it sent the email with no errors.

我正在使用 JavaMail 做一个简单的应用程序,当它在目录中找到一些文件时发送电子邮件。我设法让它从 Eclipse 工作。我运行应用程序,它发送了没有错误的电子邮件。

But, when I created the jar, and executed it, it fails in the email sending part. It gives this exception:

但是,当我创建 jar 并执行它时,它在电子邮件发送部分失败。它给出了这个例外:

javax.mail.NoSuchProviderException: No provider for smtp
 at javax.mail.Session.getProvider(Session.java:460)
 at javax.mail.Session.getTransport(Session.java:655)
 at javax.mail.Session.getTransport(Session.java:636)
 at main.java.util.MailManager.sendMail(MailManager.java:69)
 at main.java.DownloadsMail.composeAndSendMail(DownloadsMail.java:16)
 at main.java.DownloadsController.checkDownloads(DownloadsController.java:51)
 at main.java.MainDownloadsController.run(MainDownloadsController.java:26)
 at java.lang.Thread.run(Unknown Source)

I am using the library in this method:

我在这种方法中使用库:

public static boolean sendMail(String subject, String text) {

    noExceptionsThrown = true;
    try {
        loadProperties();
    } catch (IOException e1) {
        System.out.println("Problem encountered while loading properties");
        e1.printStackTrace();
        noExceptionsThrown = false;
    }

    Properties mailProps = new Properties();

    String host = "mail.smtp.host";
    mailProps.setProperty(host, connectionProps.getProperty(host));

    String tls = "mail.smtp.starttls.enable";
    mailProps.setProperty(tls, connectionProps.getProperty(tls));

    String port = "mail.smtp.port";
    mailProps.setProperty(port, connectionProps.getProperty(port));

    String user = "mail.smtp.user";
    mailProps.setProperty(user, connectionProps.getProperty(user));

    String auth = "mail.smtp.auth";
    mailProps.setProperty(auth, connectionProps.getProperty(auth));

    Session session = Session.getDefaultInstance(mailProps);
    //session.setDebug(true);

    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(messageProps.getProperty("from")));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                messageProps.getProperty("to")));

        message.setSubject(subject);
        message.setText(text);
        Transport t = session.getTransport("smtp");
        try {
            t.connect(connectionProps.getProperty("user"), passwordProps
                    .getProperty("password"));
            t.sendMessage(message, message.getAllRecipients());
        } catch (Exception e) {
            System.out.println("Error encountered while sending the email");
            e.printStackTrace();
            noExceptionsThrown = false;
        } finally {
            t.close();
        }
    } catch (Exception e) {
        System.out.println("Error encountered while creating the message");
        e.printStackTrace();
        noExceptionsThrown = false;
    }
    return noExceptionsThrown;
}

I am loading these values from properties files.

我正在从属性文件加载这些值。

mail.smtp.host=smtp.gmail.com

mail.smtp.starttls.enable=true

mail.smtp.port=587

mail.smtp.auth=true

I have tried to change the host by ssl://smtp.gmail.com, the port by 465 (just for trying something different), but it doesn't work either. Anyway, if it works fine from Eclipse with the original parameters, I guess that the values are correct, but the problem is creating the jar. I don't know very much about the possible results or changes when creating a jar. Could the JavaMail libraries someway go wrong when the jar is created?

我试图通过 ssl://smtp.gmail.com 更改主机,通过 465 更改端口(只是为了尝试不同的东西),但它也不起作用。无论如何,如果使用原始参数在 Eclipse 中工作正常,我猜这些值是正确的,但问题是创建 jar。我不太了解创建 jar 时可能的结果或更改。创建 jar 时,JavaMail 库会出错吗?

Do you have any ideas?

你有什么想法?

回答by Mike

The supporting jars: mail.jar and activation.jar are not on your classpath. When you build your jar, you need to include these on your classpath.

支持的 jars:mail.jar 和 activation.jar 不在您的类路径中。当您构建 jar 时,您需要将这些包含在您的类路径中。

回答by Maaz

I had the same problem.

我有同样的问题。

You can fix it by making the Runnable JAR file in Eclipse and using library handling options. Select the second one when making your JAR, the one that says "Package required libraries into generated JAR".

您可以通过在 Eclipse 中创建 Runnable JAR 文件并使用库处理选项来修复它。在制作 JAR 时选择第二个,即“Package required libraries into generated JAR”的那个。

package required libraries

打包所需的库

回答by Jarekczek

I managed to fix this error, which was due to a bad library. I tried to use javax.mailjar downloaded from maven, which turned out to be incomplete. So I searched once more for javamailand got one directly from Oracle(click Releaselink). This time it is a zip file.

我设法修复了这个错误,这是由于库错误造成的。我尝试使用javax.mail从maven下载的jar,结果不完整。所以我再次搜索javamail直接从 Oracle获得了一个(单击Release链接)。这次是一个zip文件。

回答by Vishal Vijayan

you need to add the smtp.jar file, I also got stuck with the same error after some google search and checking on some older projects I found that I missed this particular jar in my library, I added this jar to the library and found issue fixed.

您需要添加 smtp.jar 文件,在进行了一些谷歌搜索并检查了一些较旧的项目后,我也遇到了同样的错误,我发现我在我的库中错过了这个特定的 jar,我将这个 jar 添加到了库中并发现了问题固定的。

回答by Massi

I had the same error, updating the jar from Oracle directly fixed it.

我有同样的错误,从 Oracle 更新 jar 直接修复了它。

回答by stefan.m

For those using Gradle: you can get all required Jars as follows (1.5.5 is the latest version):

对于使用 Gradle 的用户:您可以按如下方式获取所有必需的 Jars(1.5.5 是最新版本):

    compile 'com.sun.mail:javax.mail:1.5.5'