1

我正在尝试使用 spring 调度程序发送邮件。我需要 HttpServletRequest 对象来创建 webContext,以便我可以使用百里香叶发送邮件。任何人都知道这个问题的答案。提前致谢。代码如下,

    @Async
    private void sendNotification(String toField, Users user, int currentMonth)
            throws Exception {

        // Prepare the evaluation context
        @SuppressWarnings("deprecation")

               **//here i need request object**
        final WebContext ctx = new WebContext(request, request.getSession()
                .getServletContext(), request.getLocale());

        ctx.setVariable("eagletId", user.getEagletId());
        ctx.setVariable("name", user.getFirstName());
        ctx.setVariable("setSentDate", new Date());
        ctx.setVariable("department", user.getDepartment());
        ctx.setVariable("batch", user.getBatch());

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setTo(user.getEmail());


        // create html body using thymeleaf
        final String htmlContent = this.templateEngine.process("email.html",
                ctx);
        helper.setText(htmlContent, true);
        mailSender.send(message);

    }
4

0 回答 0