tags:

views:

190

answers:

1

Hello all.

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. 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)

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?

Do you have any ideas?

Thank you very much for your help.

+1  A: 

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.

Mike
Thank you very much for answering so fast.I have just tried what you told me, I don't know if I have done it the right way:Inside Eclipse, in the properties of the Project, I have seen that in the Build Path, in Libraries: activation.jar, mailapi.jar, smtp.jar... are there. In Order and Export, I have checked these jar's. They weren't checked before. I have exported the jar again, but the error persists. May have I done something wrong? Maybe shouldn't I do it from Eclipse? Thank you Mike.
I'm not familiar with eclipse. But this is definitely something related to the classpath. You could change how you run the jar as the next step. Include the path to those two jars when you run your jar. So off the top of my head, something like: jar -cp ./full/path/to/activation.jar:./full/path/to/mail.jar -jar yourjarname.jar
Mike
Thank you Mike. I will try with this.