Java邮件:没有提供smtp [英] Java Mail : No provider for smtp

查看:108
本文介绍了Java邮件:没有提供smtp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是,当我创建了jar并执行它,它在电子邮件发送部分失败。
它提供了这个异常:

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

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)

我以这种方式使用库:

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

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

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?

你有什么想法吗?

推荐答案

支持jar:mail.jar和activation.jar不在你的类路径上。当你建立你的jar,你需要把它们包括在你的类路径。

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.

这篇关于Java邮件:没有提供smtp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆