Java:ews-java-api获取Exchange Web Services (EWS)会议日程

简介: Java:ews-java-api获取Exchange Web Services (EWS)会议日程

文档

打开官方文档,发现全是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;
    }
}

参考


相关文章
|
11月前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
874 1
|
11月前
|
存储 前端开发 Java
【JAVA】Java 项目实战之 Java Web 在线商城项目开发实战指南
本文介绍基于Java Web的在线商城技术方案与实现,涵盖三层架构设计、MySQL数据库建模及核心功能开发。通过Spring MVC + MyBatis + Thymeleaf实现商品展示、购物车等模块,提供完整代码示例,助力掌握Java Web项目实战技能。(238字)
1292 0
|
JavaScript Java 微服务
现代化 Java Web 在线商城项目技术方案与实战开发流程及核心功能实现详解
本项目基于Spring Boot 3与Vue 3构建现代化在线商城系统,采用微服务架构,整合Spring Cloud、Redis、MySQL等技术,涵盖用户认证、商品管理、购物车功能,并支持Docker容器化部署与Kubernetes编排。提供完整CI/CD流程,助力高效开发与扩展。
1208 64
|
前端开发 Java 数据库
Java 项目实战从入门到精通 :Java Web 在线商城项目开发指南
本文介绍了一个基于Java Web的在线商城项目,涵盖技术方案与应用实例。项目采用Spring、Spring MVC和MyBatis框架,结合MySQL数据库,实现商品展示、购物车、用户注册登录等核心功能。通过Spring Boot快速搭建项目结构,使用JPA进行数据持久化,并通过Thymeleaf模板展示页面。项目结构清晰,适合Java Web初学者学习与拓展。
708 1
|
缓存 NoSQL Java
Java Web 从入门到精通之苍穹外卖项目实战技巧
本项目为JavaWeb综合实战案例——苍穹外卖系统,涵盖Spring Boot 3、Spring Cloud Alibaba、Vue 3等主流技术栈,涉及用户认证、订单处理、Redis缓存、分布式事务、系统监控及Docker部署等核心功能,助你掌握企业级项目开发全流程。
1284 0
|
安全 JavaScript Java
java Web 项目完整案例实操指南包含从搭建到部署的详细步骤及热门长尾关键词解析的实操指南
本项目为一个完整的JavaWeb应用案例,采用Spring Boot 3、Vue 3、MySQL、Redis等最新技术栈,涵盖前后端分离架构设计、RESTful API开发、JWT安全认证、Docker容器化部署等内容,适合掌握企业级Web项目全流程开发与部署。
1122 0
|
人工智能 搜索推荐 IDE
突破网页数据集获取难题:Web Unlocker API 助力 AI 训练与微调数据集全方位解决方案
本文介绍了Web Unlocker API、Web-Scraper和SERP API三大工具,助力解决AI训练与微调数据集获取难题。Web Unlocker API通过智能代理和CAPTCHA绕过技术,高效解锁高防护网站数据;Web-Scraper支持动态内容加载,精准抓取复杂网页信息;SERP API专注搜索引擎结果页数据抓取,适用于SEO分析与市场研究。这些工具大幅降低数据获取成本,提供合规保障,特别适合中小企业使用。粉丝专属体验入口提供2刀额度,助您轻松上手!
978 2
|
人工智能 运维 安全
网络安全公司推荐:F5荣膺IDC全球Web应用与API防护领导者
网络安全公司推荐:F5荣膺IDC全球Web应用与API防护领导者
519 4
|
XML JSON API
Understanding RESTful API and Web Services: Key Differences and Use Cases
在现代软件开发中,RESTful API和Web服务均用于实现系统间通信,但各有特点。RESTful API遵循REST原则,主要使用HTTP/HTTPS协议,数据格式多为JSON或XML,适用于无状态通信;而Web服务包括SOAP和REST,常用于基于网络的API,采用标准化方法如WSDL或OpenAPI。理解两者区别有助于选择适合应用需求的解决方案,构建高效、可扩展的应用程序。
|
10月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。