项目代码
pom.xml 文件
Quartz 的包是整个项目不可缺少的12 3 7 84.0.6.RELEASE 4 52.2.1 69 10 15 16junit 11junit 123.8.1 13test 1417 21 22 23javax.servlet 18javax.servlet-api 193.0.1 2024 28 29javax.mail 251.4 2730 34 35 36org.quartz-scheduler 31quartz 32${quartz.version} 3337 41 42org.springframework 38spring-context-support 39${springframework.version} 4043 47 48org.springframework 44spring-tx 45${springframework.version} 4649 53 54org.springframework 50spring-web 51${springframework.version} 5255 59org.springframework 56spring-webmvc 57${springframework.version} 58
MyJob.java 继承 QuartzJobBean 然后 Override
有钻研精神的可以一点一点扒代码看 QuartzJobBean 。1 import org.quartz.JobExecutionContext; 2 import org.quartz.JobExecutionException; 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.scheduling.quartz.QuartzJobBean; 6 7 import java.util.Date; 8 9 /** 10 * kzyuan Job 参考 11 * @description black husk 12 * @description http://www.bhusk.com 13 */ 14 public class MyJob extends QuartzJobBean { 15 16 private static Logger logger = LoggerFactory.getLogger(DatabaseBackupJob.class); 17 18 @Override 19 protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { 20 21 System.out.println("执行时间:"+new Date()); 22 } 23 }
application.xml 可以理解为 quartz 配置文件 注释很齐全
1 210 11 12 13 14 24 25 2615 16 17 18 19 20 21 22 23 27 31 32 33 34 35 36 37 38 39 40 4128 29 30 42 49 5043 47 48
JavaMail 发送邮件工具类
java 实现邮件的发送, 抄送及多附件这个工具类来源网络,具体精准位置也不清楚了1 // 设置发件人 2 // mimeMsg.setFrom(new InternetAddress(from)); 3 mimeMsg.setFrom(new InternetAddress(from, nick)); 4 // 设置收件人 5 if (to != null && to.length() > 0) { 6 mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 7 } 8 // 设置抄送人 9 if (cc != null && cc.length() > 0) { 10 mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); 11 } 12 // 设置主题 13 mimeMsg.setSubject(subject); 14 // 设置正文 15 BodyPart bp = new MimeBodyPart(); 16 bp.setContent(content, "text/html;charset=utf-8"); 17 mp.addBodyPart(bp); 18 // 设置附件 19 if (fileList != null && fileList.length > 0) { 20 for (int i = 0; i < fileList.length; i++) { 21 bp = new MimeBodyPart(); 22 FileDataSource fds = new FileDataSource(fileList[i]); 23 bp.setDataHandler(new DataHandler(fds)); 24 bp.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B")); 25 mp.addBodyPart(bp); 26 } 27 } 28 mimeMsg.setContent(mp); 29 mimeMsg.saveChanges(); 30 // 发送邮件 31 if (props.get("mail.smtp.auth").equals("true")) { 32 Transport transport = session.getTransport("smtp"); 33 transport.connect((String) props.get("mail.smtp.host"), (String) props.get("username"), (String) props.get("password")); 34 transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients()); 35 transport.close(); 36 } else { 37 Transport.send(mimeMsg); 38 } 39 System.out.println("邮件发送成功"); 40 } catch (MessagingException e) { 41 e.printStackTrace(); 42 success = false; 43 } catch (UnsupportedEncodingException e) { 44 e.printStackTrace(); 45 success = false; 46 } 47 return success; 48 } 49 50 public String getMailList(String[] mailArray) { 51 StringBuffer toList = new StringBuffer(); 52 int length = mailArray.length; 53 if (mailArray != null && length < 2) { 54 toList.append(mailArray[0]); 55 } else { 56 for (int i = 0; i < length; i++) { 57 toList.append(mailArray[i]); 58 if (i != (length - 1)) { 59 toList.append(","); 60 } 61 62 } 63 } 64 return toList.toString(); 65 } 66 67 public static void main(String[] args) { 68 String from = username; 69 String[] to = {"keshu@bhusk.com", "1520812121@qq.com"}; 70 String[] copyto = {"lu12121@qq.com"}; 71 String subject = "黑壳网数据库备份"; 72 String content = "没有数据就没有一切,数据库备份就是一种防范灾难于未然的强力手段,没有了数据,应用再花哨也是镜中花水中月。"; 73 String[] fileList = new String[2]; 74 fileList[0] = "~/solo_h2/db.mv.db"; 75 fileList[1] = "~/solo_h2/db.trace.db"; 76 77 EmailManager.getInstance().sendMail(from, to, copyto, subject, content, fileList); 78 } 79 }
参考项目 GitHub: