JVM freezing while trying to send email Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JVM freezing while trying to send email without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to send an email from my program using the following method:

    void sendEmails(Tutor t, Date date, Time time, String tuteeName, String tuteeEmail){
    System.out.println("sending emails");
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");

            SimpleDateFormat timeFmt =  new SimpleDateFormat("hh:mm a");
            SimpleDateFormat dateFmt =  new SimpleDateFormat("EEE, MMMM dd");         
            String datePrint = dateFmt.format(date);
            String timePrint = timeFmt.format(time);                
            Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
                   @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

            try {

        Message tutorMessage = new MimeMessage(session);

        tutorMessage.setFrom(new InternetAddress("laneycodingclub@gmail.com"));

        tutorMessage.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(t.getEmail()));

        tutorMessage.setSubject("Tutoring Appointment Scheduled");
        tutorMessage.setText("Hey " + t.getName() + 
                            "n t You have a new appointment scheduled on "  + datePrint + " at " + timePrint + 
                            "with " + tuteeName + ". n If you cannot make this appointment, please contact club leadership immediately. "
                            + "n Thanks for helping out!");

        Transport.send(tutorMessage);

        System.out.println("Done sending");

    } catch (MessagingException e) {
                    System.out.println("messagingerror");
                    e.printStackTrace();
    }
}

But when the program reaches this method, the GUI locks up and the program freezes. This is the first time I’ve tried to use email in a Java program, so I’m not sure where the hold-up is.

Answer

You’d better avoid sending emails synchronously.

Generally it slows the work of your application, and worse – it may freeze it, for example if the mail server is unreachable or doesn’t response.

Use some asynchronous mechanisms.

I am sure in this case you have a problem with your mail server.

Use some standalone Java program to make sure that you can send emails at all with these server parameters.

We are here to answer your question about JVM freezing while trying to send email - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji