@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { RepeatStatus status = RepeatStatus.CONTINUABLE; if (chunkContext.getStepContext().getStepExecution().getReadCount() < resources.length) { // On each iteration we add as an attachment a resource Resource resource = resources[chunkContext.getStepContext().getStepExecution().getReadCount()]; helper.addAttachment(resource.getFilename(), resource.getFile()); // We confirm that we read one resource contribution.incrementReadCount(); } else { // We send the e-mail on the last iteration this.mailSender.send(helper.getMimeMessage()); // We confirm the number of attachments contribution.incrementWriteCount(resources.length); status = RepeatStatus.FINISHED; } return status; }
/** * 发送邮件 * * @param senderTitle 发送人名称, * @param senderMail 发送人邮箱地址, * @param userTitles 收件人名称列表, * @param userMails 收件人邮件地址列表, * @param mailCCs 抄送列表, * @param subject 邮件标题, * @param content 邮件内容, * @param isHtml 邮件内容是否为HTML格式 * @param fileTable * @throws MessagingException * @throws UnsupportedEncodingException */ private void send( String senderTitle, String senderMail, String[] userTitles, String[] userMails, String[] mailCCs, String[] mailBCCs, String subject, String content, boolean isHtml, Hashtable<String, File> fileTable) throws MessagingException, UnsupportedEncodingException { /* * if send mail is disabled, then return from this method. */ if (!active) return; // ------------------------------------------------------------------------------------------------------------------------------------------ MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true, DEFAULT_ENCODING); // ------------------------------------------------------------------------------------------------------------------------------------------ if (senderMail != null) { if (senderTitle != null) { mimeMessageHelper.setFrom(senderMail, senderTitle); } else { mimeMessageHelper.setFrom(senderMail); } } // ------------------------------------------------------------------------------------------------------------------------------------------ if (StringUtil.isEmpty(mailTo)) { if (userMails != null) { if (userTitles == null) { mimeMessageHelper.setTo(userMails); } else { for (int i = 0; i < userMails.length; i++) { if (i > userTitles.length || userTitles.length == 0) { mimeMessageHelper.addTo(userMails[i]); } else { mimeMessageHelper.addTo(userMails[i], userTitles[i]); } } } } if (mailCCs != null) mimeMessageHelper.setCc(mailCCs); if (mailBCCs != null) mimeMessageHelper.setBcc(mailBCCs); } else { if (log.isDebugEnabled()) { log.debug( "Option 'MailTo' is settled, mail '" + subject + "' will be sent to '" + mailTo + "'."); } mimeMessageHelper.addTo(mailTo); } // ------------------------------------------------------------------------------------------------------------------------------------------ if (subject != null) mimeMessageHelper.setSubject(subject); if (content != null) mimeMessageHelper.setText(content, isHtml); if (fileTable != null) { Enumeration<String> e = fileTable.keys(); while (e.hasMoreElements()) { String key = e.nextElement(); File file = fileTable.get(key); mimeMessageHelper.addAttachment(key, file); } } mailSender.send(mimeMessageHelper.getMimeMessage()); // ------------------------------------------------------------------------------------------------------------------------------------------ message = null; mimeMessageHelper = null; }