Quartz調度框架定時發送郵箱

Quartz調度框架定時發送郵箱

百度百科
Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它可以與J2EE與J2SE應用程序相結合也可以單獨使用,Quartz是一個完全由java編寫的開源作業調度框架,這個過程需要我們不斷的測試和學習

核心組件
1.Job–>JobDetail描述Job,任務的名稱、所屬組其他靜態信息
2、觸發器Trigger:簡單的時間觸發器(simpletrigger)、複雜的時間觸發器(crontrigger)已固定的時間間隔、固定的執行次數
3、調度器scheduler:負責調配觸發器和任務(哪一個觸發器—>哪一個任務)

準備工作

<dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.6</version>
    </dependency>
package cn.ujiuye.email;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class MailUtils {
	private static String smtp_host = "smtp.163.com";
	private static String username = "[email protected]";
	private static String password = "xl04260515";

	// 發送簡單郵件(不帶附件)
	public static void sendSimpleMail(String to, String subject, String msg) {
		Properties props = new Properties();
		props.setProperty("mail.host", smtp_host);//服務器地址
		props.setProperty("mail.transport.protocol", "smtp");//傳輸協議
		props.setProperty("mail.smtp.auth", "true");//是否要授權驗證
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				// 密碼驗證
				return new PasswordAuthentication("[email protected]", "xl123456");// 郵箱的授權碼
			}
		};
		try {
			// 1.創建會話
			Session session = Session.getInstance(props,auth);
			// 開啓Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
			session.setDebug(true);
			// 2.獲取傳輸對象
			Transport transport = session.getTransport();
			// 3.設置連接
			transport.connect("smtp.163.com", username, password);
			// 4.創建郵件
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);//郵件標題
			message.setContent(msg, "text/html;charset=utf-8");//郵件的內容
			// 5.發送郵件
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("郵件發送失敗");
		}
	}

	// 發送帶有附件的郵件
	public static void sendAttachmentMail(String to, String subject, String msg, File file) {
		Properties props = new Properties();
		props.setProperty("mail.host", smtp_host);
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.auth", "true");

		// 創建驗證器
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				// 密碼驗證
				return new PasswordAuthentication("[email protected]", "xl123456");// 郵箱的授權碼
			}
		};

		try {
			// 1.創建會話
			Session session = Session.getInstance(props, auth);
			// 開啓Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
			session.setDebug(true);
			// 2.獲取傳輸對象
			Transport transport = session.getTransport();
			// 3.設置連接
			transport.connect("smtp.163.com", username, password);
			// 4.創建郵件
			// 郵件頭
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
			// message.addRecipients(MimeMessage.RecipientType.CC,
			// InternetAddress.parse(username));
			message.setSubject(subject);
			// 郵件體
			Multipart multipart = new MimeMultipart();
			// 信息
			BodyPart content = new MimeBodyPart();
			content.setContent(msg, "text/html;charset=utf-8");
			// 添加信息
			multipart.addBodyPart(content);
			// 附件
			BodyPart attachment = new MimeBodyPart();
			String filePath = file.getPath();
			FileDataSource fileDataSource = new FileDataSource(filePath);
			attachment.setDataHandler(new DataHandler(fileDataSource));
			String filename = fileDataSource.getName();
			attachment.setFileName(MimeUtility.encodeText(filename));
			// 添加附件 作爲附件的郵件體的一部分
			multipart.addBodyPart(attachment);
			// 5.發送郵件
			message.setContent(multipart);
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("郵件發送失敗");
		}
	}

	public static void main(String[] args) {
		sendSimpleMail("[email protected]", "測試郵件", "你好呀");
		/*
		 * String path = "E:\\default.conf"; File file = new File(path);
		 * sendAttachmentMail("[email protected]", "測試郵件", "oa文檔", file);
		 */
		//sendSimpleMail("[email protected]", "測試郵件", "oa文檔");

		/*String path = "D:\\ngix.conf.txt";
		File file = new File(path);
		sendAttachmentMail("[email protected]", "週報", "helloword", file);*/
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章