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);*/
}
}