Java Mail project working when running within netbeans but not working when running directly dist/executble jar file

28 Views Asked by At

Please help, javax mail project sending email when running it from netbeans, but when running directly from executable jar file then mail not going. and also not working on any other PC and server.

note:- JOptionPane showing that message sent successful. but recipient not received any mail.

public static void sendMail(String recepient) throws Exception{
    System.out.println("Preaparing to send Email");
    Properties properties = new Properties();

    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "false");
    properties.put("mail.smtp.host", "mail.xyz.in");
    properties.put("mail.smtp.port", "587");

    

   final String myAccountEmail = Sender;
    final String password = email_pass;
    

    Session session = Session.getInstance(properties, new Authenticator(){

        @Override
        protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(myAccountEmail, password);
        }
    });

   
    Message message = prepareMessage(session,myAccountEmail,recepient);

    Transport.send(message);
    System.out.println("Message Send Successful");
    JOptionPane.showMessageDialog(null, "Successful");
    

}

private static Message prepareMessage(Session session,String myAccountEmail, String recepient) {

    try{
     MimeMessage message = new MimeMessage(session);
     message.setFrom(new InternetAddress(myAccountEmail));
     message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
     message.setSubject(sub);
   

        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText(msg);


        Multipart multipartObject = new MimeMultipart();
        multipartObject.addBodyPart(messageBodyPart1);
      
     message.setContent(multipartObject);
   
    return message;
    }
    catch(Exception e){JOptionPane.showMessageDialog(null, e.toString());
        e.printStackTrace();
   
    }
    return null;
}

package

1

There are 1 best solutions below

1
Sebastian_Ulrich On

Make sure that your code contains a valid main function as the jar file needs this as an entry point. IDEs such as netbeans sometimes make it possible to run code without an main function for faster debug purposes, but starting the jar will then not work.

public class Main {
    // your function definitions here
    
    public static void main(String[] args){
            // Your function calls here
    }
}