Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EWS from WebSite using user credentials and windows authentication for intranet

I am trying to understand how I should configure a web site (ASP.Net) that need to pass the user credential and get some mail things

I get different kind of errors depending on the app pool configure, What is the complete way to do it? I mean code + Configuration ;o)

I don't want to write the user and password but get them through the windows authentication

Thanks

code:

Service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
            {
                Url =
                    new Uri(
                    "https://xxx/yyy.asmx"),
                //UseDefaultCredentials = true doesnt help
            };
        var view = new ItemView(1820);

        // Find the first email message in the Inbox that has attachments. This results
        FindItemsResults<Item> results = Service.FindItems(folderName, view); 
        //Here I get the error 401
like image 267
Leok Avatar asked Dec 06 '25 03:12

Leok


1 Answers

Take a look at Get started with EWS Managed API client applications, it covers the basics to get you up and rolling. Note that when you use UseDefaultCredentials, you have to be on a domain joined machine, with the user logged in, and you must be targeting an on-prem server. We tested that scenario out and the following code works in that scenario.

using System;
using Microsoft.Exchange.WebServices.Data;

namespace HelloWorld
{
    class Program
    {
        static void Main()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.UseDefaultCredentials = true;
            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
            //Replace the URL below with your own, but using AutoDiscover is recommended instead
            service.Url = new Uri("https://computer.domain.contoso.com/EWS/Exchange.asmx");

            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox,
                                                               new ItemView(1));
        }
    }
}

If that doesn't work for you, then you might want to try using impersonation. See this thread, where I think they've got a similar situation . More information about impersonation is on MSDN: Impersonation and EWS in Exchange.

like image 71
Mimi Gentz Avatar answered Dec 07 '25 19:12

Mimi Gentz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!