12

I have a problem with using exchangelib in python. I try this example code:

from exchangelib import DELEGATE, Account, Credentials

creds = Credentials(
    username='xxxx\\username', 
    password="mypassword"
)

account = Account(
    primary_smtp_address='surname.name@xxxx.fr',
    credentials=creds, 
    autodiscover=True, 
    access_type=DELEGATE
)

# Print first 10 inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:10]:
    print(item.subject, item.body, item.attachments)

I tried differents usernames but nothing works and I have always the same error message :

AutoDiscoverFailed: All steps in the autodiscover protocol failed

By the way, just in case it could help, i tried to use the Exchange Web service coded for C# and it works perfectly fine with this creds, i can send a mail:

static void Main(string[] args)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    // The last parameter is the domain name
    service.Credentials = new WebCredentials("username", "password", "xxxx.lan");
    service.AutodiscoverUrl("surname.name@xxxx.fr", RedirectionUrlValidationCallback);
    EmailMessage email = new EmailMessage(service);
    email.ToRecipients.Add("surname.name@xxxx.fr");
    email.Subject = "salut ";
    email.Body = new MessageBody("corps du message");
    email.Send();
}

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    /* Validate the contents of the redirection URL. In this simple validation
       callback, the redirection URL is considered valid if it is using HTTPS
       to encrypt the authentication credentials. */
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

Thanks in advance !

Nuno André
  • 4,739
  • 1
  • 33
  • 46
Elweiss
  • 545
  • 1
  • 4
  • 12
  • 1
    Somewhere in the autodiscover negotiation procedure exchangelib is failing to pick up the right server. Unfortunately, autodiscover is quite complicated and fragile so there are lots of things that can go wrong. You can turn on DEBUG info in exchangelib and compare the steps in the output with a report from https://testconnectivity.microsoft.com/ Feel free to open an issue against exchangelib if you find out what it's doing wrong. – Erik Cederstrand Oct 02 '17 at 13:28
  • If you revealed the actual domain name behind "xxxx.fr", you might even get someone to debug this for you. The algorithm to find the right server does not require credentials. – Erik Cederstrand Oct 04 '17 at 13:26

1 Answers1

20

I finally succeed with this configuration:

from exchangelib import DELEGATE, Account, Credentials, Configuration

creds = Credentials(
    username="domain_name\\username", 
    password="password"
)

config = Configuration(server='mail.solutec.fr', credentials=creds)

account = Account(
    primary_smtp_address="my email address",
    autodiscover=False, 
    config=config,
    access_type=DELEGATE
)

For those who will have the same problem, you can find your domain_name by right-clicking on "Computer" and Properties. Username and Password are the one you use to connect to your company mailbox for example. For the server in Configuration, for me this one works: "mail.solutec.fr", where solutec is the name of my company and fr for France.

Looks like this autodiscover guy really doesn't like me ^^

Thanks for your help anyway and have a good day !

Nuno André
  • 4,739
  • 1
  • 33
  • 46
Elweiss
  • 545
  • 1
  • 4
  • 12
  • 3
    requests.exceptions.SSLError: HTTPSConnectionPool(host='xyz', port=443): Max retries exceeded with url: /EWS/Exchange.asmx (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')])"))) – amandeep1991 Jan 15 '19 at 15:06
  • @snakecharmerb: Thanks for your concern, it's now working I would be sharing the latest code soon. Code snippet which led this all working was - BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter – amandeep1991 May 19 '19 at 04:46
  • I am not able to find a specific server for my usecase. can you tell me how you idetified to use mail.solutec.fr ? – Vipendra Singh Jan 13 '21 at 06:40