简体   繁体   中英

How to connect to ExchangeServer from ASP.net (using c#)?

This is the code I use to connect to ExchangeServer. My account has 2 mailboxes assigned to it. Could this be the problem? I don't get any bug nor some result. Can someone help me, please?

static void Main(string[] args)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

    service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );


    service.AutodiscoverUrl("user@domain.com");

    FindItemsResults<Item> findResults = service.FindItems(
                WellKnownFolderName.Inbox,
                new ItemView(10));

    foreach (Item item in findResults.Items)
        Console.WriteLine(item.Subject);
}

What is the error you're getting?

EDIT:

Sorry, so you did.

So findresults is null or has Count == 0 ?

And what's with the curly braces in your code:

service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );

The problem was that I did not have AutoDiscover set up in Exchange Server. So I had to give the url in the service:

service.Url = new Uri("https://mail.domain.com/EWS/exchange.asmx");

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