2015年6月29日 星期一

quartz和java Mail範例

官網教學
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.3</version>
</dependency>



import java.util.Date;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
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;

public class SendBugzillaMail {
    public void sendBugzillaMail() {
        InternetAddress[] address = null;
        InternetAddress[] address2 = null;
        String mailserver = "SMTP server";// SMTP server
        String from = "mail";// 寄件者mail
        String to = "mail";// 收件者mail
        String cc = "mail";// 收件者mail
        String subject = "subject";// 主旨
        String messageText = "test";// 內容

        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailserver);
        props.put("mail.smtp.auth", "true");
        AuthAccountPassword msec = new AuthAccountPassword("user", "password");
        Session mailSession = Session.getDefaultInstance(props, msec);
        mailSession.setDebug(false);
        Message msg = new MimeMessage(mailSession);// 創建文件信息

        try {
            msg.setFrom(new InternetAddress(from)); // 設置傳送郵件的發信人
            address = InternetAddress.parse(to, false); // 指定收信人的信箱
            address2 = InternetAddress.parse(cc, false); // 指定收信人的信箱
            msg.setRecipients(Message.RecipientType.TO, address); // 向指定郵箱發送
            msg.setRecipients(Message.RecipientType.CC, address2); // 向指定郵箱發送
            msg.setSubject(subject);
            msg.setSentDate(new Date()); // 設定寄件時間
            msg.setText(messageText);
        } catch (AddressException e1) {
            e1.printStackTrace();
        } catch (MessagingException e1) {
            e1.printStackTrace();
        }

        try {
            System.out.println("驗證中…");
            Transport.send(msg, msg.getAllRecipients());
            System.out.println("send over!");
        } catch (AuthenticationFailedException e) {
            System.err.println("account or password error!");
        } catch (MessagingException e) {
            System.err.println("SMTP sever error!");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("send fail...");
        }
    }
}

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleSchedule implements Job {
    public static void main(String[] args) {
        JobDetail jobDetail = JobBuilder.newJob(SimpleSchedule.class).build();

        Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0 30 18 ? * MON-FRI")).startNow().build();

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = null;
        try {
            // 取得一個調度器
            scheduler = schedulerFactory.getScheduler();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void execute(JobExecutionContext paramJobExecutionContext) throws JobExecutionException {
        System.out.println("start schedule...");
        SendBugzillaMail sbm = new SendBugzillaMail();
        sbm.sendBugzillaMail();
        System.out.println("end schedule!");

    }
}

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class AuthAccountPassword extends Authenticator {
    private String name;
    private String password;

    public AuthAccountPassword(String name, String password) {
        super();
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.name, this.password);
    }
}

沒有留言:

張貼留言