0

I'm using javax.mail-1.6.1.jar to send emails from my gmail account.
It works fine when I'm running my program on eclipse, but when I run my program as an executable jar, it halts on this row - MimeMessage message = new MimeMessage(session);
No exception is thrown.

public void sendEmail(String from, String to,String subject, String body, 
String attachmentName){
  try {
      Session session = Session.getDefaultInstance(fMailServerConfig, new SMTPAuthenticator());
      Log.getInstance(Emailer.class).getLogger().error("getProperty == "+fMailServerConfig.getProperty("mail.smtp.host"));

      MimeMessage message = new MimeMessage(session);
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(subject);
      message.setSentDate(new Date());

      // Set the email msg text.
      MimeBodyPart messagePart = new MimeBodyPart();
      messagePart.setText(body);

      // Set the email attachment file
      FileDataSource fileDataSource = new FileDataSource(attachmentName);
      MimeBodyPart attachmentPart = new MimeBodyPart();
      attachmentPart.setDataHandler(new DataHandler(fileDataSource));
      attachmentPart.setFileName(fileDataSource.getName());

      // Create Multipart E-Mail.
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messagePart);
      multipart.addBodyPart(attachmentPart);

      message.setContent(multipart);
      // Send the msg. Don't forget to set the username and password
      // to authenticate to the mail server.
      Transport.send(message);
  } catch (Exception e) {
      Log.getInstance(Emailer.class).getLogger().error("Cannot send email."+ e);
      System.err.println("Cannot send email. " + e);
  }

}

sarshalom
  • 29
  • 3
  • Is there a file with a name starting with something like `hs_errpid_`? The JVM creates such a file, if the entire VM died. – Jannik May 15 '19 at 12:53
  • Does "halts" mean the program exits, or does it mean the program hangs? How do you know it halts at that line? What does the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug) show? – Bill Shannon May 15 '19 at 16:29
  • @Jannik no, I did not find this file – sarshalom May 16 '19 at 21:02
  • @Bill Shannon yes, exits. JavaMail debug doesn't show nothing – sarshalom May 16 '19 at 21:30
  • It doesn't show "*Cannot send mail*" in your console? Nothing at all appears in your console? – Vince May 16 '19 at 22:17
  • If the JavaMail debug output shows literally nothing, then you're not even getting to the method above. If the debug output actually shows something, edit your post to include the output. Also, try catching Throwable instead of Exception to see if the JVM is failing. And again, how do you know which lines causes it to exist? Have you added print statements around the line that fails? – Bill Shannon May 17 '19 at 03:19
  • @sarshalom Well, then the problem is probably not caused by the JVM dying entirely. Have you tried to use a debugger? That's probably the easiest way to find the location where it exits. – Jannik May 17 '19 at 12:40
  • @Vince Emigh no...just exit – sarshalom May 20 '19 at 18:37
  • @Bill Shannon Ok, I catched a Throwable and logged it, so I got this: java.lang.NoClassDefFoundError: javax/activation/DataHandler Yes, I found the failing line with print statements – sarshalom May 20 '19 at 18:50
  • @Jannik I can't debug it because it runs fine in the eclipse, the trouble is the executable jar – sarshalom May 20 '19 at 18:51
  • Fixed! I've added javax.activation.jar to the project. Thanks everybody for you help., I couldn't do it with you. – sarshalom May 20 '19 at 19:56

0 Answers0