1

I Am Having a problem, I think it is a logical one. As I don't fully understand the concept of Quartz.

I am Having the following Classes: a- BirthdayServlet.java, b- QuartzJob.java, c- MailSenderBean.java

The Goal is to send Email using Quartz Scheduler in my Servlet. And here is the classes code.

a-BirtdayServlet class

@WebServlet("/BirthdayServlet")
public class BirthdayServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BirthdayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
        protected void doGet(HttpServletRequest request, HttpServletResponse 
    response)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: 
    ").append(request.getContextPath());
        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.getWriter().println("I am Entering");

        JobDetail job = JobBuilder.newJob(QuartzJob.class).build();
        Trigger t1 = TriggerBuilder.newTrigger().withIdentity("SimpleTrigger").startNow().build();
        Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
        sc.start();
        sc.scheduleJob(job, t1);

        response.getWriter().println("I am Leaving");

    }

}

b- QuartzJob.java class

public class QuartzJob implements Job {
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        String body = ("TryingMyQuartz");
        String subject = ("Recieved?");

        String EmpEmail = ("amrr@gmail.com");

        String sender = ("moha123@gmail.com");
        String password = ("012344");

        String senderDomain = ("@gmail.com");
        String senderUsername = ("moha123");

        MailSenderBean mailsender = new MailSenderBean();
        mailsender.sendEmail(sender, senderUsername, password, EmpEmail, subject, body, senderDomain);

    }

}

c- MailSenderBean.java

public class MailSenderBean {

    public void sendEmail(String sender, String senderUsername, String password, String EmpEmail, String subject,
            String body, String senderDomain) {
        Properties props = System.getProperties();

        if (senderDomain.equals("@gmail.com")) {
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.fallback", "false");

            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(true);

            Message mailMessage = new MimeMessage(mailSession);
            try {
                mailMessage.setFrom(new InternetAddress(sender));
            } catch (MessagingException e) {
                // Sender mail not found

            }
            try {
                mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(EmpEmail));
            } catch (MessagingException e) {
                // Recipent mail not found

            }

            try {
                mailMessage.setContent(body, "text/html");
                mailMessage.setSubject(subject);
                Transport transport = mailSession.getTransport("smtps");
                transport.connect("smtp.gmail.com", senderUsername, password);
                transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
            } catch (MessagingException e) {
                // Cannot transport that message Sorry!

            }

        }
    }

}

I am a bit sure that MailSenderBean is working fine. So the problem is with the Servlet or the QuartzJob classes. I don't know what is the problem with my code exactly. But it is not working. And the response.getwirter("I am Leaving") is not displayed. So the program terminates while making the schedule Job or Quartz.

A.K.
  • 2,284
  • 3
  • 26
  • 35
Amr Ahmed
  • 31
  • 1
  • 4
  • Wrap the section you think is failing in a try/catch and catch `Throwable`. See if you're getting an error there. – stdunbar Jul 29 '17 at 15:11
  • That's What i receive: java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:793) at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:757) at welcome.MailSenderBean.sendEmail(MailSenderBean.java:70) at welcome.QuartzJob.execute(QuartzJob.java:95) Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.BEncoderStream at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at – Amr Ahmed Jul 29 '17 at 15:36
  • Not sure what your build environment looks like but see [this post](https://stackoverflow.com/questions/16807758/java-lang-noclassdeffounderror-com-sun-mail-util-maillogger-for-junit-test-case) to make sure you have all of the Java Mail dependencies at both compile and run time. – stdunbar Jul 29 '17 at 16:38

0 Answers0