0

I'm trying to run a jar file which sends a mail, using this command:

java -jar sendmail.jar

When I run the code from NetBeans everything works fine, but when I export to a jar file and run the jar file I get the following error:

Error Sending mailjavax.mail.MessagingException: Could not connect to SMTP host:
 smtp.<xxx>.com, port: 25;
  nested exception is:
        java.net.SocketException: Permission denied: connect

After I got the error I executed the code from NetBeans, and the mail was sent. Thus I don't have any issue with SMTP or connection.

I think that the jar file cannot locate the mail.jar. Please advise.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Shann
  • 660
  • 1
  • 6
  • 19
  • have you set proper classpath for your jar? – Abubakkar Sep 30 '13 at 06:37
  • If you created `.jar` from netbeans then you don't have to worry about supporting `mail.jar` file. It copy supported `.jar` file in lib folder next to your `.jar` file. – Vicky Thakor Sep 30 '13 at 06:39
  • If it runs fine in netbeans and not after build then it must be some kind of import not included into the jar file.. – JWqvist Sep 30 '13 at 06:41
  • maybe a duplicate: http://stackoverflow.com/questions/15484940/unable-to-send-a-mail-using-javamail-api – Simiil Sep 30 '13 at 06:45
  • How you are creating `.jar` file? – Vicky Thakor Sep 30 '13 at 06:48
  • @Abu - yes class path is properly set. – Shann Sep 30 '13 at 06:48
  • @VickyThakor - i just clean and build the project – Shann Sep 30 '13 at 06:49
  • http://stackoverflow.com/questions/8360913/weird-java-net-socketexception-permission-denied-connect-error-when-running-groo – Vicky Thakor Sep 30 '13 at 06:54
  • Is there any chance that you are using different JVMs in NetBeans and in console to run jar? There are known issue with JDK7: http://stackoverflow.com/a/12902187/1430055 – Maxim Kolesnikov Sep 30 '13 at 06:54
  • @MaksimKolesnikov - i think this may be the issue. i see that the jvm on netbeans is 1.6. and on console is 1.7.how to change it to 1.7 on netbeans? – Shann Sep 30 '13 at 07:00
  • In Netbeans, right click on the project > properties > Libraries and choose a JDK 7 Java Platform. After that you'll see this error both in console and in NetBeans. To fix it you should explicitly define VM option `-Djava.net.preferIPv4Stack=true`. – Maxim Kolesnikov Sep 30 '13 at 07:08

3 Answers3

1

Does your machine by any chance use ipv6? if it does, you can try to start your jar with the -Djava.net.preferIPV4Stack=true argument.

Simiil
  • 2,281
  • 1
  • 22
  • 32
0

Be sure that the mail.jar is probably imported under the libs and your not just referenced it.

JWqvist
  • 717
  • 6
  • 15
0

Be sure to put your mail.jar file into lib folder and also ensure that your lib folder and sendmail.jar files are present in same directory.