EWS
按照cron表達式自動執(zhí)行尋找地址存放某個時間段的文件
package com.smile.schedule;
import java.util.HashSet;
import java.util.Set;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.smile.ews.EwsMailAttService;
import com.smile.ews.EwsMailVO;
@Component
@EnableScheduling
public class ScheduledTask {
? ? // 兩種表達式:Seconds Minutes Hours DayofMonth Month DayofWeek Year或
? ? // Seconds Minutes Hours DayofMonth Month DayofWeek
? ? // 每隔5秒執(zhí)行一次:*/5 * * * * ?
? ? // 每隔1分鐘執(zhí)行一次:0 */1 * * * ?
? ? // 每天23點執(zhí)行一次:0 0 23 * * ?
? ? // 每天凌晨1點執(zhí)行一次:0 0 1 * * ?
? ? // 每月1號凌晨1點執(zhí)行一次:0 0 1 1 * ?
? ? // 每天0點運行一次 0 0 0 * * ?
// 每天晚上七點執(zhí)行 "0 0 19 * * ?"
? ? // 每一天凌晨1點執(zhí)行一次
//? ? @Scheduled(cron = "0 */1 * * * ?")
? ? @Scheduled(cron = "0 0 19 * * ?")
? ? public void reportCurrentByCron() {
? ? ? ? EwsMailAttService service = new EwsMailAttService();
? ? ? ? EwsMailVO ewsMail = new EwsMailVO();
? ? ? ? ewsMail.setUrl("https://autodiscover.boshi.com.cn/ews/Exchange.asmx");
? ? ? ? ewsMail.setUsername("CFETS");
? ? ? ? ewsMail.setPassword("123QWEasd");
? ? ? ? Set<String> targetFiles = new HashSet<>();
? ? ? ? //文件存放的地址\\\\ia\\temp\\liuxt\\1
? ? ? ? targetFiles.add("\\\\ia\\temp\\liuxt\\1");
? ? ? ? // targetFiles.add("\\\\ia\\temp\\liuxt\\1\\");
? ? ? ? // targetFiles.add("\\\\ia\\temp\\liuxt\\1\\2\\3");
? ? ? ? // targetFiles.add("\\\\ia\\temp\\liuxt\\2\\3");
? ? ? ? ewsMail.setTargetFiles(targetFiles);
? ? ? ? service.initMethod(ewsMail);
? ? ? ? try {
service.saveAtt(ewsMail);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? }
}
EwsEmail格式
package com.smile.ews;
import java.util.Set;
import lombok.Data;
@Data
public class EwsMailVO {
? ? /**
? ? * 郵件服務(wù)器地址
? ? */
? ? private String url;
? ? /**
? ? * 郵箱賬戶
? ? */
? ? private String username;
? ? /**
? ? * 郵箱密碼
? ? */
? ? private String password;
? ? /**
? ? * 目標(biāo)文件
? ? */
? ? private Set<String> targetFiles;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<String> getTargetFiles() {
return targetFiles;
}
public void setTargetFiles(Set<String> targetFiles) {
this.targetFiles = targetFiles;
}
}
主要過程
package com.smile.ews;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.CollectionUtils;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.enumeration.search.LogicalOperator;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.Attachment;
import microsoft.exchange.webservices.data.property.complex.AttachmentCollection;
import microsoft.exchange.webservices.data.property.complex.FileAttachment;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import microsoft.exchange.webservices.data.search.filter.SearchFilter;
public class EwsMailAttService {
? ? private ExchangeService service = null;
? ? @PostConstruct
? ? public void initMethod(EwsMailVO ewsMail) {
? ? ? ? service = new ExchangeService(ExchangeVersion.Exchange2010);
? ? ? ? ExchangeCredentials credentials = new WebCredentials("CFETS", "123QWEasd");
? ? ? ? service.setCredentials(credentials);
? ? ? ? try {
? ? ? ? ? ? service.setUrl(new URI("https://autodiscover.boshi.com.cn/ews/Exchange.asmx"));
? ? ? ? } catch (URISyntaxException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void saveAtt(EwsMailVO ewsMail) throws Exception {
? ? ? ? Set<String> targetfiles = ewsMail.getTargetFiles();
? ? ? ? if (CollectionUtils.isEmpty(targetfiles)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? // 質(zhì)押式回購市場交易情況總結(jié)日報_20180816
? ? ? ? ItemView view = new ItemView(Integer.MAX_VALUE);
? ? ? ? Date currentTime = new Date();
? ? ? ? SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//日期格式
? ? ? ? String startTime = formatter.format(currentTime)+" 00:00:00";//當(dāng)前最小日期
? ? ? ? String endTime = formatter.format(currentTime)+" 23:59:59";//當(dāng)前最大日期
? ? ? ? SearchFilter start = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"));
? ? ? ? SearchFilter end = new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"));
? ? ? ? SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, start, end);
? ? ? ? FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, filter, view);
? ? ? ? ArrayList<Item> items = findResults.getItems();
? ? ? ? Set<String> xls = new HashSet<>();
? ? ? ? for (Item item : items) {
? ? ? ? ? ? EmailMessage message = EmailMessage.bind(service, item.getId());
? ? ? ? ? ? if (message.getHasAttachments()) {
? ? ? ? ? ? ? ? // TODO 如果沒有附件會否報錯
? ? ? ? ? ? ? ? AttachmentCollection atts = message.getAttachments();
? ? ? ? ? ? ? ? for (Attachment attachment : atts) {
? ? ? ? ? ? ? ? ? ? // 附件可能是郵件,也可能是文件。郵件的情況應(yīng)該遞歸,但是業(yè)務(wù)上暫時遇不到,所以這里只處理文件類型的附件
? ? ? ? ? ? ? ? ? ? if (attachment instanceof FileAttachment) {
? ? ? ? ? ? ? ? ? ? ? ? FileAttachment att = (FileAttachment) attachment;
? ? ? ? ? ? ? ? ? ? ? ? // 復(fù)制到指定的多個目錄
? ? ? ? ? ? ? ? ? ? ? ? for (String targetfile : targetfiles) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Path path = Paths.get(targetfile);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 層層創(chuàng)建目錄
? ? ? ? ? ? ? ? ? ? ? ? ? ? Files.createDirectories(path);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 將文件名追加到路徑
? ? ? ? ? ? ? ? ? ? ? ? ? ? path = path.resolve(att.getName());
? ? ? ? ? ? ? ? ? ? ? ? ? ? att.load();
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 需要判斷是否xls
? ? ? ? ? ? ? ? ? ? ? ? ? ? InputStream is = new ByteArrayInputStream(att.getContent());
? ? ? ? ? ? ? ? ? ? ? ? ? ? HSSFWorkbook workbook = new HSSFWorkbook(is);
? ? ? ? ? ? ? ? ? ? ? ? ? ? FileOutputStream os = new FileOutputStream(path.toFile());
? ? ? ? ? ? ? ? ? ? ? ? ? ? workbook.write(os);
? ? ? ? ? ? ? ? ? ? ? ? ? ? os.close();
? ? ? ? ? ? ? ? ? ? ? ? ? ? workbook.close();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? service.close();
? ? }
}
springboot啟動
package com.smile;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CopyEmailApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(CopyEmailApplication.class, args);
? ? }
}
POM配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>ewas</groupId>
? <artifactId>email</artifactId>
? <version>0.0.1-SNAPSHOT</version>
? <name>email</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<springboot.mybatis.version>1.3.0</springboot.mybatis.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
<quartz.version>2.2.3</quartz.version>
<commons-lang3.version>3.6</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-exec.version>1.3</commons-exec.version>
<fastjson.version>1.2.37</fastjson.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>${commons-exec.version}</version>
</dependency>
<!-- Quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency>
<!-- Json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<!-- EWS -->
<!-- <dependency> -->
<!-- <groupId>com.microsoft.ews-java-api</groupId> -->
<!-- <artifactId>ews-java-api</artifactId> -->
<!-- <version>1.1.1</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>commons-httpclient</groupId> -->
<!-- <artifactId>commons-httpclient</artifactId> -->
<!-- <version>3.1</version> -->
<!-- </dependency> -->
<!-- Lombox -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<!-- Ews -->
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
<!-- poi -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.poi</groupId>
? ? ? ? ? ? <artifactId>poi</artifactId>
? ? ? ? ? ? <version>3.16</version>
? ? ? ? </dependency>
</dependencies>
<build>
<finalName>email</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>