李李的技術(shù)博客

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            13 隨筆 :: 0 文章 :: 61 評(píng)論 :: 0 Trackbacks

          看看下面的應(yīng)用例子,程序執(zhí)行三秒后會(huì)在后臺(tái)開始發(fā)Email,只有幾行程序,很簡單吧。
          本來就應(yīng)該這么簡單,還能再省么,呵呵,謝謝開源的力量……

              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是一個(gè)利用Opensymphony Quartz做簡單的調(diào)度,其中EmailJob實(shí)現(xiàn)了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 李李 閱讀(2109) 評(píng)論(0)  編輯  收藏 所屬分類: 技術(shù)
          主站蜘蛛池模板: 大方县| 濮阳市| 沅陵县| 湖州市| 新建县| 惠来县| 吴桥县| 松江区| 东阳市| 漳浦县| 朝阳区| 宁陕县| 平邑县| 金寨县| 阿拉尔市| 金昌市| 凤阳县| 礼泉县| 浦江县| 磴口县| 增城市| 五寨县| 临清市| 上林县| 澳门| 大竹县| 黑水县| 翼城县| 珠海市| 大荔县| 广宗县| 汽车| 马鞍山市| 南澳县| 常德市| 蒙山县| 龙里县| 新野县| 静安区| 平利县| 武胜县|