如何在ews中获取消息entryid

时间:2018-12-19 04:23:05

标签: c# api exchange-server-2010

对不起,英语不好。 我使用Exchange版本2010 SP1。 我从公用文件夹获取消息,我需要从此消息中获取entryid,例如Outlook interop:

var service = ConnectToService();
var folder = GetFolder(service, "SomePublicFolder");
var items = folder.FindItems(new ItemView(1));
foreach (EmailMessage item in items)
{
    var entryIdLikeOutlook = ????????
}

我需要这种格式的ID:

000000001A447390AA6611CD9BC800AA002FC45A09001207274E1F9F314E9787FCD1B7E58F07000137B94C9100001207274E1F9F314E9787FCD1B7E58F07000137E300B60000

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。 连接到服务示例:

public static class ConnectionToEws
    {
        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            bool result = false;
            var redirectionUri = new Uri(redirectionUrl);
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }
            return result;
        }

        public static ExchangeService ConnectToService()
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
            {
                Credentials = new WebCredentials("Login", "Password")
                ,UseDefaultCredentials = false
            };
            service.AutodiscoverUrl("EmailAddress", RedirectionUrlValidationCallback);
            return service;
        }
    }

获取EntryId示例:

static void Main(string[] args)
        {
            var service = ConnectionToEws.ConnectToService();
            var results = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
            foreach (var item in results)
            {
                var originalId = new AlternateId(IdFormat.EwsId, item.Id.ToString(), "EmailAddress", false);
                var entryId = service.ConvertId(originalId, IdFormat.HexEntryId);
                Console.WriteLine(((AlternateId)entryId).UniqueId);
            }
            Console.ReadKey();
        }

因公共田而异:

static void Main(string[] args)
        {
            var service = ConnectionToEws.ConnectToService();
            var someFolder = GetFolder(service, "MyFolderName");
            var results = someFolder.FindItems(new ItemView(1));
            foreach (var item in results)
            {
                var alternatePublicFolderItem = new AlternatePublicFolderItemId(IdFormat.EwsId, someFolder.Id.UniqueId, item.Id.ToString());
                var convertResult = service.ConvertId(alternatePublicFolderItem, IdFormat.HexEntryId);
                Console.WriteLine(((AlternatePublicFolderItemId)convertResult).ItemId);
            }

            Console.ReadKey();
        }

        private static Folder GetFolder(ExchangeService service, string folderName)
        {
            var folderView = new FolderView(int.MaxValue);
            var findFolderResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView);
            foreach (var folder in findFolderResults)
            {
                if (folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return folder;
                }
            }
            return null;
        }