1

I'm trying to send a mail through Java Mail with the following code:

public boolean sendMail(String recipients, String subject, String message) {
        try {
            System.out.println("Starting with sending..");
            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "false");
            props.put("mail.smtp.ssl.enable", "true");
            System.out.println("Opening session");
            Session session = Session.getDefaultInstance(props, new SocialAuth());
            System.out.println("Creating Message");
            session.setDebug(true);
            System.out.println("Session is " + session.toString());
            Message msg = new MimeMessage(session);
            System.out.println("Getting InternetAddress");
            InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);
            System.out.println("Setting info");
            msg.setFrom(from);

            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

            msg.setSubject(subject);
            msg.setContent(message, "text/plain");
            Transport transport = session.getTransport("smtp");
            System.out.println("Connecting to smtp server");
            transport.connect(SMTP_HOST, FROM_ADDRESS, PASSWORD); //host, 25, "myemailhere", "mypasshere");
            msg.saveChanges();
            System.out.println("About to transport message");
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Mail sent");
            return true;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            return false;

        } catch (MessagingException ex) {
            ex.printStackTrace();
            return false;
        }
    }

This works just fine when running from IntelliJ. However, when I run the .jar file it gets stuck at the line Message msg = new MimeMessage(session).

I've tried changing class-paths of the .jars. Tried catching any exceptions that may be thrown, but none were. JavaMail debug only returns the current version, 1.6.2, but nothing else. I can't seem to figure out why MimeMessage does not get created.

Bas Jansen
  • 35
  • 5

2 Answers2

1

It is possible to be a missing dependency for mime, or anything related. Look at IntelliJ's Run tab and see if you have something missing in you jar.

First you need to know if you created a .jar file properly

See a question related here

If generated correctly you should execute it as:

java -jar jarfile.jar

Another interesting point is:

So many parts of the Java ecosystem, depend on configuration files that describes how the environment should be established and it's entirely common for developers to forget to copy the configuration file alongside the JAR file.

From here

Roberto Gonçalves
  • 3,186
  • 4
  • 13
  • 27
  • I've followed exactly that order to create the .jar file. The 'Output Layout' tab has: server.jar
    - Extracted 'javax.mail.jar'
    - Extracted 'jdatepicker-1.3.4.jar'
    - 'project' compile output
    Underneath I have added javax.mail.jar and jdatepicker-1.3.4 added to the Class Path. Main Class is Server.ServerGUI. Is it possible that due to the jars being located in the top project folder and the code output in src/Server/ServerGUI something goes wrong during the compilation of the .jar?
    – Bas Jansen Aug 23 '19 at 08:15
  • Yes, it is pretty possible! – Roberto Gonçalves Aug 23 '19 at 11:50
1

I've switched the project over to a maven project, letting maven build the dependencies for me and now everything works fine. I'm going to assume that something within the .jar building of IntelliJ was going wrong, but for now this works.

Bas Jansen
  • 35
  • 5