当前位置: 首页 > 工具软件 > EWS-Client > 使用案例 >

EWS(Exchange Service)基本使用(Java)

章侯林
2023-12-01

EWS(Exchange Service)基本使用(获取个人会议,会议室会议内容,会议室列表,发送会议,修改会议,删除会议)

最近公司要求和exchange服务对接,所以稍微研究了一下官方文档,做出以下总结,欢迎大家补充。

先放出exchange官方开发说明文档:https://docs.microsoft.com/zh-cn/exchange/client-developer/exchange-web-services/calendars-and-ews-in-exchange

  1. 与exchange web服务器建立连接
// 与exchange web服务器建立连接
		ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
		ExchangeCredentials credentials = new WebCredentials("you email", "password");
		service.setCredentials(credentials);
		// 这个url可能每个公司不一样,大多数是这个
		service.setUrl(new URI("https://outlook.office365.com/ews/exchange.asmx"));
		service.setTraceEnabled(true);
  1. 获取会议内容
		Calendar start = Calendar.getInstance();
		start.set(2019,10,1);
		Calendar end = Calendar.getInstance();
		end.set(2020,2,1);
		CalendarFolder calendar = CalendarFolder.bind(service, WellKnownFolderName.Calendar, new PropertySet());
		CalendarView cView = new CalendarView(start.getTime(), end.getTime());
		// 指定要查看的邮箱
		cView.setPropertySet(new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End));
		// Retrieve a collection of appointments by using the calendar view.
		FindItemsResults<Appointment> appointments = calendar.findAppointments(cView);

		for (Appointment a : appointments)
		{
			// 获取各个会议信息
		}
  1. 获取会议室会议内容(会议室某天各个会议开始时间,结束时间,主题和发件人)
List<AttendeeInfo> attendees = new ArrayList<>();
        attendees.add(new AttendeeInfo("会议室邮箱", MeetingAttendeeType.Room, true));
        GetUserAvailabilityResults results = service.getUserAvailability(attendees,
                // 设置当天时间
                new TimeWindow(DateTime.now().plusDays(0).toDate(), DateTime.now().plusDays(1).toDate()),
                AvailabilityData.FreeBusy);
        List<Map<String, Object>> list = new ArrayList<>();
        for (AttendeeAvailability availability : results.getAttendeesAvailability())
        {
            for (CalendarEvent calEvent : availability.getCalendarEvents())
            {
                Map<String, Object> map = new HashMap<>();
                // 开始时间和结束时间
                map.put("start", calEvent.getStartTime());
                map.put("end", calEvent.getEndTime());
                CalendarEventDetails details = calEvent.getDetails();
                if(details != null){
                    // subject中包含发件人和主题
                    String subject = details.getSubject();
                    if(StringUtils.isNotBlank(subject)){
                        // 按空格区分发件人和主题
                        String[] strings = subject.split(" ");
                        map.put("booker", strings[0]);
                        map.put("meetingName", strings[1]);
                    }
                }
                list.add(map);
            }
        }
  1. 获取会议室列表
try {
			// 这里先获取大的会议室列表,大多数是按区域分的,不如公司有五个办公地点,这里获取的就是各个办公地点的address
			EmailAddressCollection myRoomLists = service.getRoomLists();
			List<String> addList = new ArrayList<String>();
			for (EmailAddress address : myRoomLists) {
				EmailAddress emailAddress = new EmailAddress(address.getAddress());
				// 根据各个办公地点的address,再获取具体的所有会议室信息
				// 如获取北京办公地的所有会议室信息
				Collection<EmailAddress> list = service.getRooms(emailAddress);
				for (EmailAddress e : list) {
					addList.add(e.getAddress());
					System.out.println(e.getName());
					System.out.println(e.getNamespace());
				}
			}
			return addList;
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
  1. 发送会议
Appointment appointment = null;
		try {
			appointment = new Appointment(service);
			appointment.setSubject("会议主题");
			appointment.setBody(MessageBody.getMessageBodyFromText("会议消息体"));
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// 会议时间,如果使用会议室的话,会议时间一定是会议室空闲的时间,否则创建失败
			appointment.setStart(format.parse("2020-01-06 11:30:00"));
			appointment.setEnd(format.parse("2020-01-06 14:30:00"));
            
			appointment.setLocation("会议位置");
			// 必须参加的员工的账号
			// 这里如果会议需要用到会议室的话,一定要添加会议室的邮箱
			appointment.getRequiredAttendees().add("会议室邮箱");
			appointment.getRequiredAttendees().add("参与人邮箱1");
			appointment.getRequiredAttendees().add("参与人邮箱2");
		
			// 可选参加的员工的账号
//			appointment.getOptionalAttendees().add("abc@456.com");
			// 会议提前15分钟提醒
			appointment.setReminderMinutesBeforeStart(15);
			appointment.save(SendInvitationsMode.SendToAllAndSaveCopy);
//			appointment.update(ConflictResolutionMode.AutoResolve);
			System.out.println(appointment.getId());
			Item item = Item.bind(service, appointment.getId(), new PropertySet(ItemSchema.Subject));
			// 这里创建一个item之后,如果要修改会议,一定要保存好这个item.getId(),否则找不到会议,不能修改
			System.out.println("会议创建成功: " + item.getSubject());
		} catch (Exception e) {
			e.printStackTrace();
		}
  1. 修改会议
// itemId即创建会议室生成的唯一itemId
		Appointment appointment = Appointment.bind(service, 
				ItemId.getItemIdFromString(itemId), new PropertySet());
		appointment.setSubject("会议主题21");
		appointment.setBody(MessageBody.getMessageBodyFromText("会议消息体21"));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		appointment.setStart(format.parse("2020-01-06 11:30:00"));
		appointment.setEnd(format.parse("2020-01-06 14:30:00"));

		appointment.setLocation("会议位置");
		// 必须参加的员工的账号
		appointment.getRequiredAttendees().add("邮箱");
		appointment.getRequiredAttendees().add("邮箱");
		// 可选参加的员工的账号
//			appointment.getOptionalAttendees().add("abc@456.com");
		appointment.setReminderMinutesBeforeStart(15);
		// Send the update request to the Exchange server.
		appointment.update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToAllAndSaveCopy);
		System.out.println(appointment.getId());
		Item item = Item.bind(service, appointment.getId(), new PropertySet(ItemSchema.Subject));
		System.out.println("会议更新成功: " + item.getSubject());
  1. 取消会议
Appointment appointment = Appointment.bind(service, 
				ItemId.getItemIdFromString(itemId), new PropertySet());
		// Delete the meeting by using the Delete method.
		// 取消后,之前创建的会议标题前会加上已取消三个字
		appointment.delete(DeleteMode.MoveToDeletedItems, SendCancellationsMode.SendToAllAndSaveCopy);

转自:https://blog.csdn.net/weixin_42196915/article/details/104021751

 类似资料: