李李的技术博客

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  13 随笔 :: 0 文章 :: 61 评论 :: 0 Trackbacks

看看下面的应用例子,程序执行三秒后会在后台开始发Email,只有几行程序,很简单吧。
本来就应该这么简单,还能再省么,呵呵,谢谢开源的力量……

    SimpleEmail email = new SimpleEmail();
    email.addTo(
"receiver@somemail.com""Receier's Name");
    email.setSubject(
"Email from www.bba96.com");
    email.setMsg(
"Hello, guy!");
    EmailScheduler emailScheduler 
= new EmailScheduler();
    emailScheduler.process(email);

这里用到了jakarta common email中的SimpleEmail
EmailScheduler是一个利用Opensymphony Quartz做简单的调度,其中EmailJob实现了Quartz的Job接口
以下是EmailScheduler以及EmailJob源代码。

package com.bba96.scheduler;

import java.util.Date;

import javax.mail.Authenticator;

import org.apache.commons.mail.Email;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;

public class EmailScheduler {

    
public void process(Email email, Authenticator authenticator)
            
throws SchedulerException {
        
// TODO if can be optimized with static instance.
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
        Scheduler sched 
= schedFact.getScheduler();
        sched.start();

        JobDetail jobDetail 
= new JobDetail("EmailJob"null, EmailJob.class);
        jobDetail.getJobDataMap().put(EmailJob.EMAIL, email);
        jobDetail.getJobDataMap().put(EmailJob.AUTHENTICATIOR, authenticator);
        
//Create a trigger that fires exactly once, three seconds from now
        long startTime = System.currentTimeMillis() + 3000L;
        SimpleTrigger trigger 
= new SimpleTrigger("emailTrigger"null,
                
new Date(startTime), null00L);
        sched.scheduleJob(jobDetail, trigger);
    }

    
public void process(Email email) throws SchedulerException {
        process(email, 
null);
    }

}

 

package com.bba96.scheduler;

import javax.mail.Authenticator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class EmailJob implements Job {

    
protected final Log logger = LogFactory.getLog(EmailJob.class);

    
public static String EMAIL = "EMAIL";

    
public static String AUTHENTICATIOR = "AUTHENTICATIOR";

    
public static String DEFAULT_HOST = "your smtp mail server";

    
public static int DEFAULT_SMTP_PORT = 25;

    
public static String DEFAULT_USER = "yourmail@yourserver.com";

    
public static String DEFAULT_PASSWORD = "your password";

    
public static String DEFAULT_FROM_ADDRESS = "yourmail@yourserver.com";

    
public static String DEFAULT_FROM_NAME = "Your Name";

    
public void execute(JobExecutionContext context)
            
throws JobExecutionException {
        Email email 
= (Email) context.getJobDetail().getJobDataMap().get(EMAIL);
        
if (email != null) {
            Authenticator authenticator 
= (Authenticator) context
                    .getJobDetail().getJobDataMap().get(AUTHENTICATIOR);
            
if (email.getHostName() == null) {
                email.setHostName(DEFAULT_HOST);
            }
            
if (email.getSmtpPort() == null) {
                email.setSmtpPort(DEFAULT_SMTP_PORT);
            }
            
if (authenticator == null) {
                authenticator 
= new DefaultAuthenticator(DEFAULT_USER,
                        DEFAULT_PASSWORD);
                email.setAuthenticator(authenticator);
            }
            
if (email.getFromAddress() == null) {
                
try {
                    email.setFrom(DEFAULT_FROM_ADDRESS, DEFAULT_FROM_NAME);
                } 
catch (EmailException e) {
                    logger.error(
"Email address invalid", e);
                    
return;
                }
            }
            
try {
                email.send();
            } 
catch (EmailException e) {
                logger.error(
"Email send error", e);
            }
        }
    }

}

posted on 2005-10-15 22:01 李李 阅读(2077) 评论(0)  编辑  收藏 所属分类: 技术

只有注册用户登录后才能发表评论。


网站导航: