有个任务是等运维要找我要文件的时候去生产ftp下载下来给他,嗯嗯,每天,好繁琐,
偷偷的本地跑了个定时,每天11点执行jar包,jar包的功能就是给运维发送邮件,ok。
1.mac系统自带定时任务功能:crontab -e编辑定时任务
00 11 * * * cd /Users/yp-tc-m-7129/newworkspace/ftpFileEmail/target && java -jar ftpFileEmail-0.0.1-SNAPSHOT.jar
2.ftpFileEmail-0.0.1-SNAPSHOT.jar包实现
采用spring-maill实现比javax.mail的方式代码简洁,1->新建项目,添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Freemarker 依赖,发送HTML格式的邮件的方式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 连接ftp -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
3.具体实现:
1->需要项目在启动时自动执行发送邮件的功能,需要继承一个
CommandLineRunner,功能就是对 java -jar 中传过来的参数做一个校验,没有的时候就默认当天,然后在调用发送邮件功能的biz
@Component
public class MyRunner implements CommandLineRunner {
@Value("${startDate}")
private String beginDateStr = "";
@Value("${endDate}")
private String endDateStr = "";
@Autowired
private SendEmailService sendEmailService;
private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);
@Override
public void run(String... strings) throws Exception {
System.out.println("项目启动了1111");
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate beginDate;
LocalDate endDate;
if(CheckUtils.isEmpty(beginDateStr) && CheckUtils.isEmpty(endDateStr)) {
beginDate = LocalDate.now().plusDays(-1);
endDate = LocalDate.now().plusDays(-1);
}else {
if(!CheckUtils.verifyDate(beginDateStr) || !CheckUtils.verifyDate(endDateStr)) {
logger.error("initMerchantFileMD5Str 日期参数格式不正确 beginDateStr={} endDateStr={}", beginDateStr,endDateStr);
return;
}
beginDate = LocalDate.parse(beginDateStr, df);
endDate = LocalDate.parse(endDateStr,df);
}
String subject="邮件主题";
String content="邮件内容";
String[] receiveArray = {"qian.wen@jia007.com"};
while(beginDate.isBefore(endDate) || beginDate.equals(endDate)) {
Email email = new Email(receiveArray, subject, content, null, null);
sendEmailService.sendFileMail(email,beginDate);
beginDate = beginDate.plusDays(1);
}
}
}
然后发送附件部分使用spring-boot-mail
@Autowired
private JavaMailSender mailSender;
public void sendHtmlSimpleMail() throws Exception {
LOGGER.info("准备发送HTML邮件");
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("2413172711@qq.com");
helper.setTo("2413172711@qq.com");
helper.setSubject("HTML主题");
helper.setText("<html><body><img src=\"cid:springcloud\" >"
+ "</br>"
+"哈哈!"
+ "</body></html>",true);
// 发送图片
File file = ResourceUtils.getFile("classpath:static/image/WechatIMG1.jpeg");
helper.addInline("springcloud", file);
// 发送附件
file = ResourceUtils.getFile("classpath:static/file/a.txt");
helper.addAttachment("附件名称", file);
mailSender.send(message);
}
接下来就可以看到每天11点定时给运维同事发送一份带有附件的文件啦,附件来源于ftp,涉及公司ftp代码不做讲述。