2

i try to use : Microsoft.Exchange.WebServices.dll to use outlook. but connection return error

Error return line:service.AutodiscoverUrl("myusernamek@xxxx.com");

The Autodiscover service could not be located. my codes:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Connect to Exchange Web Services as user1 at contoso.com.
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Credentials = new WebCredentials("myusernamek@xxxx.com", "mypassword", "xxxx.com");
                service.TraceEnabled = true;
                service.AutodiscoverUrl("myusernamek@xxxx.com");

                // Create the e-mail message, set its properties, and send it to user2@contoso.com, saving a copy to the Sent Items folder. 
                EmailMessage message = new EmailMessage(service);
                message.Subject = "Interesting";
                message.Body = "The proposition has been considered.";
                message.ToRecipients.Add("recipientname@xxxx.aero");
                message.SendAndSaveCopy();

                // Write confirmation message to console window.
                Console.WriteLine("Message sent!");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadLine();
            }

        }
    }
}

alt text

Alfred Myers
  • 6,384
  • 1
  • 40
  • 68
Penguen
  • 16,836
  • 42
  • 130
  • 205
  • 4
    Screenshot is perfectly readable, open the image in a new window http://i.stack.imgur.com/ul780.png – Seph Nov 03 '11 at 08:05
  • instruction how to convert EWS to latest .net platforms is here https://stackoverflow.com/a/74213274/1704458 I know people are looking for this – T.S. Oct 27 '22 at 23:09

5 Answers5

2

I know this is an old question, but recently wrestled with this and similar looking error (including ISA server). It was fixed with:

service.EnableScpLookup = false;

This was not required when working with an explicit URL, but was when using AutoDiscover

quick_dry
  • 811
  • 5
  • 11
1

This is a common problem , Autodiscover service error is encountered when this autodiscover service by exchange is down

Resolution is to provide actual URL for exchange location , rather than autodiscovering it.

This solved my same issue.

Gurpreet Singh
  • 1,641
  • 3
  • 17
  • 29
0

The code suggest that you have an Exchange 2007 server... Is it properly configured for using the Autodiscover features? Confirm that you can ping autodiscover.XXXX.com and view https://autodiscover.XXXX.com in a web browser.

Alternately, you may need to use your internal domain name for autodiscovery and login. For example, in my office the external email addresses are on a domain like CompanyX.com, but the internal Active Directory domain is like CompanyX.local, and we do not have autodiscover on the open Internet, so my EWS needs to locate Autodiscover.CompanyX.local.

ewall
  • 27,179
  • 15
  • 70
  • 84
0

this is an old post but maybe someone will need it. do not use auto discover, it is rly slow.

how to find your exchange url:

  • -open your outlook application and connect to your exchange
  • -hold Ctrl key and right click on the outlook icon in the system tray
  • -select "test e-mail auto configuration"
  • -click the test button
  • -look for the following line:

enter image description here

oh and to use the url you trop that extra line of code:

service.Url = new Uri("your url here");
Dave
  • 120
  • 1
  • 10
-2

try these concept:

private static ExchangeService getService(String userEmail, String login, String password, String hostName)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); 
    AutodiscoverService auservice = new AutodiscoverService(hostName);
    if (auservice.ServerInfo != null) 
    {
        try
        {
            service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback);
        }
        catch (AutodiscoverRemoteException ex)
        {
            Console.WriteLine("Exception thrown: " + ex.Error.Message);
        }

    }
    else
    {
        service.Url = new Uri("https://" + hostName + "/EWS/Exchange.asmx");
    }

    service.UseDefaultCredentials = true;


    if (service.ServerInfo == null)
    {
        service.Credentials = new WebCredentials(login, password);
    }
    return service;
}
vrajs5
  • 4,066
  • 1
  • 27
  • 44
Aksndr
  • 1
  • 2