Java Servlet,使用Quartz发送邮件

时间:2017-07-29 14:46:49

标签: java servlets java-ee quartz-scheduler quartz

我有问题,我认为这是合乎逻辑的问题。因为我不完全理解Quartz的概念。

我有以下类:a- BirthdayServlet.java,b- QuartzJob.java,c- MailSenderBean.java

目标是在我的Servlet中使用Quartz Scheduler发送电子邮件。这是类代码。

a-BirtdayServlet类

@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类

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!

            }

        }
    }

}

我确信MailSenderBean工作正常。所以问题在于Servlet或QuartzJob类。我不知道我的代码到底有什么问题。但它没有用。并且不显示response.getwirter(“我正在离开”)。因此程序在制定Job或Quartz时会终止。

0 个答案:

没有答案