定时发送发送邮件

更多的查看这里:http://ramonblog.cloudfoundry.com/

继续上次在MySql中定义的Event Scheduler的功能,如果Event执行过程中出错,要求发送邮件到指定的邮箱。由于MySql不提供发送邮件的功能(至少我也没有找到相关的信息),想着由Web系统定时检查EventLog表,Event执行失败将插入数据到这个表,如果发现有错误信息,则发送邮件,之后再将这些EventLog中相应的错误信息给清除掉,当然只是修改标示符,而不是真正的删除。

定时检查

对于Servlet,可以实现ServletContextListener,然后在启动的时候定义java.util.Timer来调度java.util.TimerTask执行特定的任务。显然这个是很容易理解的。

发送邮件

发送邮件可以利用我这篇文章里面提到的Java Mail的包,可以很简单。但是呢,由于我是用AWS,AWS for Java的库里面自带了JavaMail包,所以我就直接运用这个了,但是其复杂程度是前者的2-3倍。具体可以参考后面的代码。我这里参考了这篇文章http://qing.weibo.com/tj/678f0ab83300029n.html。由于这里用的是Gmail,如果直接用它的方法,会抛出一个错误“530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74”,为了解决它,我查找到StackOverflow的一个解决方法。 你也可以从官方的FAQ中找到答案。

import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


/**
 * Application Lifecycle Listener implementation class AggregationEventChecker
 *
 */
public class AggregationEventChecker implements ServletContextListener {
    private Timer timer = new Timer();

    /**
     * Default constructor.
     */
    public AggregationEventChecker() {

    }

    /**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
    //立即启动,间隔1小时
        timer.schedule(new AggregationEventCheckTimerTask(), 0, 3600 * 1000);
    }

    /**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
    //销毁timer
        timer.cancel();
    }

    private class AggregationEventCheckTimerTask extends TimerTask {

        @Override
        public void run()
        {
            sendEmail()
        }

        private boolean sendEmail()
        {
            try
            {
                Properties props = new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", 25);
                props.put("mail.smtp.auth", "true");

                String userName = "ramon@gmail.com";
                String password = "yourpassword";

                Session session = Session.getDefaultInstance(props, null);
                Message msg = new MimeMessage(session);

                Address from = new InternetAddress("llmfei@gmail.com");
                Address to = from;
                msg.setFrom(from);
                msg.setRecipient(Message.RecipientType.TO, to);

                msg.setSubject("Test");
                msg.setSentDate(new Date());
                msg.setText('Test Email');

                Transport transport = session.getTransport("smtps");
                transport.connect(props.getProperty("mail.smtp.host"), userName, password);
                transport.sendMessage(msg, new Address[] {to});
                transport.close();

                return true;
            }
            catch (AddressException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (MessagingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值