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