【问题标题】:Email sending in Spring batchSpring批量发送邮件
【发布时间】:2014-09-14 12:59:03
【问题描述】:

我对 Spring 批处理非常陌生。在处理一些记录后,我需要从我的应用程序中发送邮件。经历了很多链接。但我没有发现任何有用的东西。有人可以帮帮我吗?

【问题讨论】:

标签: email jakarta-mail sendmail send spring-batch


【解决方案1】:

您需要实现一个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()方法中发送邮件;您可以根据需要使用任何您喜欢的方式发送电子邮件。

【讨论】:

    【解决方案2】:

    嗨,你可以试试下面的代码,我在我的项目中使用这个 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()");
    
            }
    
    }
    

    【讨论】:

    • Spring Batch有什么关系?
    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2018-08-30
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多