看看下面的应用例子,程序执行三秒后会在后台开始发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), null, 0, 0L);
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);
}
}
}
}