0

I am creating a larger program that will have emails sent to the users account whenever an event happens, and right now I am just focusing on making the email sending work.

Right now, I have it functioning perfectly in the IDE (IntelliJ) with no errors or warnings, but after I jar the file and run it in the terminal I get an error every time the program tries to send an email.

I am assuming I jarred the file wrong since it works in the IDE perfectly fine, but I am not too sure. I have looked up similar issues to mine but have not found a working solution.

This is the file that has issues in the terminal

package handler;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.Properties;

public class Sender {

    private Sender(){}

    private static final String SENDERS_GMAIL = "myemail@email.com";
    private static final String SENDERS_PASSWORD = "mypassword";

    private static final String RECEIEVES_EMAIL = "myemail@email.com";

    private static Properties mailServerProperties;
    private static Session mailSession;
    private static MimeMessage mailMessage;

    public static void sendMail(String emailBody) throws Throwable
    {
        mailServerProperties = System.getProperties();
        mailServerProperties.put("mail.smtp.port", "587");
        mailServerProperties.put("mail.smtp.auth", "true");
        mailServerProperties.put("mail.smtp.starttls.enable", "true");

        mailSession = Session.getDefaultInstance(mailServerProperties);

        mailMessage = new MimeMessage(mailSession);
        mailMessage.addRecipient(RecipientType.BCC, new InternetAddress(RECEIEVES_EMAIL));
        mailMessage.setSubject("Test Email");
        mailMessage.setContent(emailBody, "text/html");

        Transport transport = mailSession.getTransport("smtp");
        transport.connect("smtp.gmail.com", SENDERS_GMAIL, SENDERS_PASSWORD);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        transport.close();
    }
}

And whenever I run the .jar in the terminal, this is the error I get:

    C:\Users\genlap\EmailSender>java -jar SendEmail.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 handler.Sender.sendMail(Sender.java:37)
        at handler.ManageService.run(ManageService.java:32)
        at java.lang.Thread.run(Unknown Source)
Message failed to be sent.

The line in the Sender file that is being called out in the error is

Transport transport = mailSession.getTransport("smtp");

Does anybody know how I can solve this?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
brent_mb
  • 337
  • 1
  • 2
  • 14
  • See if the answers to [this question](https://stackoverflow.com/questions/2980408/java-mail-no-provider-for-smtp) help. – PM 77-1 Mar 07 '19 at 15:36
  • @PM77-1 unfortunately no that did not help. I believe I already have all the required dependencies properly added to the `.jar`. But I could always be wrong – brent_mb Mar 07 '19 at 16:03

2 Answers2

1

You are either not including the dependencies in the SendEmail.jar or it lacks MANIFEST.MF entries which point to them. Due to that dependencies which IntelliJ is using for building and running the application are not available when you are executing java -jar from the command line.

The easiest way would be to create a fat JAR with all dependencies. If you use Maven you can take a look at this answer or use Maven Shade Plugin.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks for the response. I created a "fat" JAR as you described, but the issue still persists. I am guessing the issue is with the `MANIFEST.MF` now, but since this is an automatically generated file I am not sure how I could fix it. Do you have any suggestions? – brent_mb Mar 07 '19 at 15:57
  • What build tools are you using to create the JAR? Maven? Or are you using IntelliJ? – Karol Dowbecki Mar 07 '19 at 16:03
  • I am using IntelliJ – brent_mb Mar 07 '19 at 16:08
0

If you're creating your application by taking the class files out of the JavaMail jar file and putting them in your application jar file, then you're missing all the META-INF files from the JavaMail jar file. The best approach is to find a solution that does not require repackaging the JavaMail jar file, such as One-JAR.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I am using the JavaMail jar files as a whole, I am not taking the individual class files out and adding them to the application. I have a lot of .jar `dependencies` that I am just telling the `IDE` to pack in to the outputted application file I am creating – brent_mb Mar 07 '19 at 22:41
  • How is the IDE adding these jar files to the application file? What do you see if you look inside the application jar file that is created? – Bill Shannon Mar 08 '19 at 22:35