电子邮件发送在Spring批次

时间:2014-07-23 11:08:23

标签: email javamail sendmail send spring-batch

我是Spring批次的新手。我需要在处理一些记录后从我的应用程序发送邮件。通过许多链接。但我没有找到任何有用的东西。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您好您可以尝试下面的代码,我在我的项目中使用这个javax代码并且工作很酷..

public void sendMailtoMgr(final String subject, final String message,
        String mgrmailIds) {

    String mngrecipients = null;

    Message msg = null;
    InternetAddress[] mgraddress = null;
    boolean debug = false;

    try {
      // Load your SMTP Properties from Property file
        Properties props = new Properties();
        props.put(SMTP_HOST, SMTP_HOST_VALUE);
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
        msg = new MimeMessage(session);
      // From value is nothing but from Address , can give your email id
        msg.setFrom(new InternetAddress(SMTP_FROM_VALUE));

        mngrecipients = mgrmailIds;
        mgraddress = addRecipients(mngrecipients);
        if (mgraddress != null && mgraddress.length != 0) {
        msg.setRecipients(Message.RecipientType.TO, mgraddress);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setSubject(subject);
        msg.setContent(message, "text/html");
        Transport.send(msg);

        }
    }
    catch (MessagingException mex) {
        logger.info("Exception in sendMail()");
        mex.printStackTrace();
        } 
    catch (Exception e) {
         logger.info("Exception in sendMail()", e);


    } finally {
         logger.info("Exiting sendMail()");

        }

}

答案 1 :(得分:0)

您需要实施JobExecutionListener并按以下方式将其添加到您的工作中:

<batch:job id="provisionAddOns" >
    <batch:step id="cpsProvisionAddOns">
       ...
    </batch:step>
    <batch:listeners>
        <batch:listener>
            <bean class="EmailNotification" />
        </batch:listener>
    </batch:listeners>
</batch:job>

此处EmailNotification实施JobExecutionListener并以afterJob()方式发送电子邮件;您可以根据自己的需要使用任何您喜欢的方式发送电子邮件。