EWS exception while trying to read email from exchange

Dec 16 2022 6:44 AM

I am facing an issue while reading emails from my exchange email account using EWS Microsoft API.

Please help me with a solution if anybody has

Used below code

static void Main(string[] args)

        {

            string UserName = "**";

            string Password = "**";

            string EXCHANGEMAILURL = @"<Exchange Webservice URL>";

            ExchangeService service = new ExchangeService();

            service.Timeout = 500000;

            service.Credentials = new NetworkCredential(UserName, Password);

            service.Url = new Uri(EXCHANGEMAILURL);

            try

            {

                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

                ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

                FindFoldersResults folderSearchResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(int.MaxValue));

                Folder xChangeSourceFolder = folderSearchResults.Folders.ToList().Find(

                         f => f.DisplayName.Equals("Inbox", StringComparison.CurrentCultureIgnoreCase));

                ItemView itemView = new ItemView(50);

                FindItemsResults<Item> findIncomingMails = service.FindItems(xChangeSourceFolder.Id, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false),

                     new ItemView(50));

                Item item = findIncomingMails.First();                

                EmailMessage messageHTML = (EmailMessage)item;   

                messageHTML.Load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments, ItemSchema.Body) { RequestedBodyType = BodyType.HTML });

                string htmlString = messageHTML.Body.Text.ToString();

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception Occurred : " + ex.StackTrace);

            }

        }

The exception

Unable to connect to the remote server

A connection attempt failed because the connected party did not properly respond after a period of time,or established connection failed because connected host has failed to respond


Answers (1)