C# ExChange WES API

1.准备 Microsoft.Exchange.WebServices.dll 和 Microsoft.Exchange.WebServices.Auth.dll

2.调用对应方法

 class Program
    {
        static void Main(string[] args)
        {
            // Validate the server certificate.
            // For a certificate validation code example, see the Validating X509 Certificates topic in the Core Tasks section.

            try
            {
                // Connect to Exchange Web Services as user1 at contoso.com.
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                service.Credentials = new WebCredentials("邮箱@outlook.com", "密码,");
                service.AutodiscoverUrl("[email protected]");

                // Create the e-mail message, set its properties, and send it to [email protected], saving a copy to the Sent Items folder. 
                EmailMessage message = new EmailMessage(service);
                message.Subject = "Interesting";
                message.Body = "The proposition has been considered.";
                message.ToRecipients.Add("发送的邮件");

                //发送邮件
                message.Send();
                Console.WriteLine("Message sent!");
                Console.ReadLine();

                Appointment appointment = new Appointment(service);
                appointment.Subject = "Status Meeting";
                appointment.Body = "The purpose of this meeting is to discuss status.";
                appointment.Start = new DateTime(2017, 12, 27, 16, 0, 0); //年月日 时分秒
                appointment.End = appointment.Start.AddHours(2);
                appointment.Location = "Conf Room";
                appointment.RequiredAttendees.Add("发送的邮件1");
                appointment.RequiredAttendees.Add("发送的邮件2");
                //订会议
                appointment.Save(SendInvitationsMode.SendOnlyToAll);


                // 查询对应日程
                DateTime startDate = DateTime.Now.AddDays(-1);
                DateTime endDate = startDate.AddDays(30);
                const int NUM_APPTS = 5;

                // Initialize the calendar folder object with only the folder ID. 
                CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

                // Set the start and end time and number of appointments to retrieve.
                CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);

                // Limit the properties returned to the appointment's subject, start time, and end time.
                cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

                // Retrieve a collection of appointments by using the calendar view.
                FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

                Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
                                  " to " + endDate.Date.ToShortDateString() + " are: \n");

                foreach (Appointment a in appointments)
                {
                    //显示当前有的日程
                    Console.Write("Subject: " + a.Subject.ToString() + " ");
                    Console.Write("Start: " + a.Start.ToString() + " ");
                    Console.Write("End: " + a.End.ToString());
                    Console.WriteLine();
                }


                //拿到用户消息
                 AutodiscoverService autodiscoverService = new AutodiscoverService("outlook.office365.com");
                autodiscoverService.Credentials = new NetworkCredential("[email protected]", "liange0819jia,", "outlook.office365.com");

                //提交请求并获取设置。响应只包含所请求的
                //设置,如果它们存在的话。

                GetUserSettingsResponse userresponse = autodiscoverService.GetUserSettings(
                "[email protected]",
                UserSettingName.UserDN,
                UserSettingName.UserMSOnline,  //是否在线
                UserSettingName.UserDisplayName, //显示名称
                UserSettingName.InternalEcpVoicemailUrl,
                UserSettingName.InternalEcpEmailSubscriptionsUrl,
                UserSettingName.EcpVoicemailUrlFragment,
                UserSettingName.EcpEmailSubscriptionsUrlFragment,
                UserSettingName.ExternalEcpVoicemailUrl,
                UserSettingName.ExternalEcpEmailSubscriptionsUrl,
                UserSettingName.AlternateMailboxes,
                UserSettingName.ActiveDirectoryServer,
                UserSettingName.UserDeploymentId,
                UserSettingName.InternalMailboxServer,
                UserSettingName.MailboxDN,
                UserSettingName.PublicFolderServer,
                UserSettingName.ActiveDirectoryServer,
                UserSettingName.ExternalMailboxServer,
                UserSettingName.EcpDeliveryReportUrlFragment,
                UserSettingName.EcpPublishingUrlFragment,
                UserSettingName.EcpTextMessagingUrlFragment,
                UserSettingName.ExternalEwsUrl,
                UserSettingName.CasVersion,
                UserSettingName.EwsSupportedSchemas

               );

                Console.WriteLine(userresponse);
                foreach (KeyValuePair<UserSettingName, Object> usersetting in userresponse.Settings)
                {
                    Console.WriteLine(usersetting.Key.ToString() + ": " + usersetting.Value);
                }



                // Get the number of items in the contacts folder. To limit the size of the response, request only the TotalCount property.
                ContactsFolder contactsfolder = ContactsFolder.Bind(service,
                                                                    WellKnownFolderName.Contacts,
                                                                    new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.TotalCount));

                // Set the number of items to the number of items in the Contacts folder or 50, whichever is smaller.
                int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;

                // Instantiate the item view with the number of items to retrieve from the Contacts folder.
                ItemView view = new ItemView(numItems);

                // To keep the response smaller, request only the display name.
                view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, ContactSchema.DisplayName,
                    ContactSchema.Birthday, 
                    ContactSchema.JobTitle, 
                    ContactSchema.Alias,
                    ContactSchema.AssistantName,
                    //ContactSchema.EmailAddresses,
                    //ContactSchema.PhoneNumbers,
                    ContactSchema.ManagerMailbox); //EmailAddresses  PhoneNumbers 不能在这里加入请求

                // Request the items in the Contacts folder that have the properties that you selected.
                FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);
                
                foreach (Item item in contactItems)
                {
                    if (item is Contact)
                    {
                        Contact contact = item as Contact;
                        Console.WriteLine(contact.DisplayName);
                        // Console.WriteLine(contact.PhoneNumbers);
                        Console.WriteLine(contact.JobTitle);
                        Console.WriteLine(contact.Alias);
                        Console.WriteLine(contact.AssistantName);
                        Console.WriteLine(contact.ManagerMailbox);
                        NameResolutionCollection nd = service.ResolveName(contact.EmailAddresses[EmailAddressKey.EmailAddress1].Address);

                        //    foreach (NameResolution nm in nd)
                        //{
                        //        if (nm.Mailbox.RoutingType =="SMTP")
                        //    {
                        //           Console.WriteLine ( nm.Mailbox.Address); 
                        //        }
                        //    }

                        //      Console.ReadLine();
                        //    //   Console.WriteLine(contact.AssistantName);
                    }
                }

               

                Console.WriteLine(contactItems);
               Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadLine();
            }
        }


    }

猜你喜欢

转载自blog.csdn.net/LuHan_/article/details/79989962