文档
- https://learn.microsoft.com/zh-cn/exchange/client-developer/exchange-server-development
- https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide
打开官方文档,发现全是C#的示例
我们的实际情况是:
- 开发端在MacOS
- 调试在Windows虚拟机,通过虚拟机连接内网,获取内网部署的Exchange Web Services数据
- 最终服务端需要运行在Linux上
为了多端兼容,而且对C#不熟悉,还好网上还能搜索到部分Java的示例,所以使用Java接口进行二次开发
主要开发功能是获取用户的日历数据,同步到公司系统,方便查看统计数据
Maven
<dependency> <groupId>com.microsoft.ews-java-api</groupId> <artifactId>ews-java-api</artifactId> <version>2.0</version> </dependency>
部分代码
业务接口
package com.example.demo.service; import microsoft.exchange.webservices.data.core.service.item.Appointment; import microsoft.exchange.webservices.data.core.service.item.Contact; import java.util.Date; import java.util.List; public interface OutlookService { /** * 翻页获取会议 * @param emailName * @param pageNumber * @param pageSize * @return */ List<Appointment> getAppointmentListForPage(String emailName, int pageNumber, int pageSize); /** * 按照时间区间获取会议 * @param emailName * @param startDate * @param endDate * @return */ List<Appointment> getAppointmentListForDate(String emailName, Date startDate, Date endDate); /** * 获取所有联系人列表 * @param emailName * @return */ List<Contact> getAllContactList(String emailName); }
接口实现
package com.example.demo.service.impl; import com.example.demo.service.OutlookService; import lombok.extern.slf4j.Slf4j; import microsoft.exchange.webservices.data.core.ExchangeService; import microsoft.exchange.webservices.data.core.PropertySet; import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName; import microsoft.exchange.webservices.data.core.service.item.Appointment; import microsoft.exchange.webservices.data.core.service.item.Contact; import microsoft.exchange.webservices.data.core.service.item.Item; import microsoft.exchange.webservices.data.credential.ExchangeCredentials; import microsoft.exchange.webservices.data.credential.WebCredentials; import microsoft.exchange.webservices.data.property.complex.FolderId; import microsoft.exchange.webservices.data.property.complex.Mailbox; import microsoft.exchange.webservices.data.search.CalendarView; import microsoft.exchange.webservices.data.search.FindItemsResults; import microsoft.exchange.webservices.data.search.ItemView; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * exchange 版本 2019 */ @Service @Slf4j public class OutlookServiceImpl implements OutlookService { @Value("${outlook.exchangeUrl}") private String exchangeUrl; @Value("${outlook.username}") private String username; @Value("${outlook.password}") private String password; /** * 获取服务 * * @return */ private ExchangeService getExchangeService() { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); ExchangeCredentials credentials = new WebCredentials(username, password); service.setCredentials(credentials); try { service.setUrl(new URI(exchangeUrl)); } catch (URISyntaxException e) { e.printStackTrace(); } service.setTraceEnabled(true); return service; } /** * 在 Exchange 中使用 EWS 获取约会和会议 * https://learn.microsoft.com/zh-cn/exchange/client-developer/exchange-web-services/how-to-get-appointments-and-meetings-by-using-ews-in-exchange * <p> * EWS Java API 的基本使用 * https://blog.csdn.net/m0_37972348/article/details/83960690 */ @Override public List<Appointment> getAppointmentListForPage(String emailName, int pageNumber, int pageSize) { List<Appointment> list = new ArrayList<>(); List<Item> items = this.getItemsForPage(emailName, WellKnownFolderName.Calendar, pageNumber, pageSize); for (Item item : items) { if (item instanceof Appointment) { Appointment appointment = ((Appointment) item); try { appointment.load(PropertySet.FirstClassProperties); } catch (Exception e) { e.printStackTrace(); } list.add(appointment); } } return list; } /** * 获取所有联系人列表 */ @Override public List<Contact> getAllContactList(String emailName) { List<Contact> list = new ArrayList<>(); int pageNumber = 1; int pageSize = 50; while (true) { List<Item> items = this.getItemsForPage(emailName, WellKnownFolderName.Contacts, pageNumber, pageSize); if (items == null || items.size() == 0) { break; } for (Item item : items) { if (item instanceof Contact) { Contact contactItem = ((Contact) item); list.add(contactItem); } } if(items.size() < pageSize){ break; } pageNumber++; } return list; } public List<Item> getItemsForPage(String emailName, WellKnownFolderName folderName, int pageNumber, int pageSize) { ExchangeService service = this.getExchangeService(); List<Item> list = new ArrayList<>(); int offset = (pageNumber - 1) * pageSize; System.out.println("pageNumber: " + pageNumber); System.out.println("offset: " + offset); ItemView view = new ItemView(pageSize, offset); FolderId folderId = new FolderId(folderName, new Mailbox(emailName)); FindItemsResults<Item> findResults = null; try { findResults = service.findItems(folderId, view); } catch (Exception e) { log.info("items is empty"); } if (findResults == null) { return list; } int totalCount = findResults.getTotalCount(); System.out.println("totalCount: " + totalCount); //MOOOOOOST IMPORTANT: load messages' properties before try { service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties); } catch (Exception e) { log.info("service.loadPropertiesForItems error"); } list = findResults.getItems(); return list; } @Override public List<Appointment> getAppointmentListForDate(String emailName, Date startDate, Date endDate) { ExchangeService service = this.getExchangeService(); List<Appointment> list = new ArrayList<>(); CalendarView calendarView = new CalendarView(startDate, endDate); FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(emailName)); FindItemsResults<Appointment> findResults = null; try { findResults = service.findAppointments(folderId, calendarView); } catch (Exception e) { log.info("Appointments is empty"); } if (findResults == null) { return list; } int totalCount = findResults.getTotalCount(); System.out.println("totalCount: " + totalCount); list = findResults.getItems(); for (Appointment appointment : list) { try { appointment.load(PropertySet.FirstClassProperties); } catch (Exception e) { e.printStackTrace(); } } return list; } }
参考