简体   繁体   中英

Read email from outlook - problem with XML

I'm trying to connect to outlook online and download some emails with predefined conditions.

in browser when I go to https://outlook.office.com/mail/ and logon with specific email and pass, I can see all mails.

I have c# console application that should connect, search and manipulate with emails.

    public void EmailReceiver()
    {
        try
        {
            ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
            exchangeService.Credentials = new NetworkCredential("username", "pass");
            exchangeService.Url = new Uri("https://outlook.office.com/mail/");

            var inbox = Folder.Bind(exchangeService, WellKnownFolderName.Inbox);

            var sf1 = new SearchFilter.ContainsSubstring(EmailMessageSchema.From, "searchMail");

            SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.Or);

            searchFilterCollection.Add(sf1);

            var view = new ItemView(1000);

            var findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, view);

            return;

            //foreach (Item item in findResults)
            //{
            //    GetAttachmentsFromEmail(item.Id);
            //}
        }

        catch (Exception ex)
        {
            throw;
        }
    }

But cannot connect to outlook 在此处输入图像描述

Any suggestions?

Office 365 no longer allows basic authentication, you need to switch to OAuth2. You can still allow basic auth, but you must explicit do so in the Exchange admin console for your tenant. But even in that case, you need to use WebCredentials class, not NetworkCredential . In case of OAuth, you need OAuthCredentials class.

You might also want to set TraceEnabled property to true and provide your class (assign it to the TraceListener property) to log all EWS calls.

exchangeService.TraceEnabled = true;
exchangeService.TraceListener = new MyTraceListener(this);
exchangeService.TraceFlags = TraceFlags.All;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM