title: Spring Batch email listener
body: <p>I want to configure Spring Batch to send an email when a job has began and also send an email when a job has finished or failed. I've seen some examples by searching such as the link here <a href="https://blog.codecentric.de/en/2011/09/email-sending-with-spring-batch/" rel="nofollow noreferrer">on codecentric</a> but I don't use XML annotation in my project.</p>
  
  <p>I have a listener that looks like this, but not sure what other pieces I may need. Any input would be appreciated</p>
  
  <pre><code>public class SendMailListener implements JobExecutionListener {
  
      final static Logger LOGGER = LoggerFactory.getLogger(SendMailListener.class);
  
      @Autowired
      private JobExplorer explorer;
  
      @Autowired
      private StepExecution stepExecution;
  
      @Override
      public void afterJob(JobExecution jobExecution) {
          if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
              LOGGER.info("The job has been completed with the following parameters: " + jobExecution.getJobParameters());
          }
          //send email here
  
      }
  
      @Override
      public void beforeJob(JobExecution jobExecution) {
          // TODO Auto-generated method stub
  
      }
      //send email here
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I have the job defined with the listener: </p>
  
  <pre><code>@Bean(name = "mccINSTANCETESTjob")
      public Job mccOrz004job(JobBuilderFactory jobFactory, //
              @Qualifier("instanceTestSetupStep") Step orz004SetupStep, //
              @Qualifier("callM204Step") Step callM204Step, //
              @Autowired SingleInstanceListener listener, @Autowired SendMailListener mailListener) { //
          return jobFactory.get("mccINSTANCETESTjob") //
                  .incrementer(new RunIdIncrementer()) //
                  .listener(mailListener) //
                  .start(orz004SetupStep) //
                  .next(callM204Step) //
                  .build();
      }
  </code></pre>