Logo

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

photo

2020年11月17日

最近公司要求和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);
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1821 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1803 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1533 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,686 作者:Pastore Antonio
1532 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1509 浏览量