李李的技術博客

            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 李李 閱讀(2108) 評論(0)  編輯  收藏 所屬分類: 技術
          主站蜘蛛池模板: 龙里县| 竹北市| 乌苏市| 宽城| 汾西县| 荣昌县| 治多县| 南开区| 延寿县| 沭阳县| 龙泉市| 安国市| 竹北市| 蓬安县| 沂水县| 东光县| 吕梁市| 大悟县| 西乌珠穆沁旗| 双辽市| 苏尼特右旗| 龙山县| 五寨县| 宁德市| 西城区| 通许县| 大安市| 龙南县| 龙里县| 石楼县| 长宁区| 威海市| 都江堰市| 水城县| 师宗县| 阳山县| 日土县| 株洲市| 惠州市| 巨野县| 天峨县|