MAIF / melusine

📧 Melusine: Use python to automatize your email processing workflow
https://maif.github.io/melusine
Other
352 stars 58 forks source link

Exchange connexion error #91

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi ! I tried to do a connexion to Exchange with the ExchangeConnector class from melusine. But when I ran the program the following error appeared :

exchangelib.errors.AutoDiscoverFailed: All steps in the autodiscover protocol failed for email 'my_adress@verlingue.fr'. If you think this is an error, consider doing an official test at https://testconnectivity.microsoft.com

So, I tried to modify the melusine class in order to force the connexion. Here is the code :

class ExchangeConnector:

    def __init__(
        self,
        login_address: str,
        password: str,
        mailbox_address: str = None,
        max_wait: int = 60,
        routing_folder_path: str = None,
        correction_folder_path: str = None,
        done_folder_path: str = None,
        target_column="target",
    ):
        """
        Parameters
        ----------
        login_address: str
            Email address used to login and send emails.
        password: str
            Password to login to the Exchange mailbox
        mailbox_address: str
            Email address of the mailbox. By default, the login address is used
        max_wait: int
            Maximum time (in s) to wait when connecting to mailbox
        routing_folder_path: str
            Path of the base routing folder
        correction_folder_path: str
            Path of the base correction folder
        done_folder_path: str
            Path of the Done folder
        target_column: str
            Name of the DataFrame column containing target folder names
        """
        self.login_address = login_address
        self.mailbox_address = mailbox_address or login_address
        self.folder_list = None
        self.target_column = target_column

        # Connect to mailbox
        self.credentials = Credentials(self.login_address, password)
        self.exchangelib_config = Configuration(server= 'mail.company.domain', retry_policy=FaultTolerance(max_wait=max_wait), credentials=self.credentials, auth_type=NTLM)
        #add the server name, the auth_type. These parameters can be found in outlook with 'auto configuration test'
        # Mailbox account (Routing, Corrections, etc)
        self.mailbox_account = Account(
            primary_smtp_address=self.mailbox_address,
            credentials=self.credentials,
            autodiscover=False,
            #turn autodiscover off
            config=self.exchangelib_config,
            access_type=DELEGATE
            #acces_type can, as well as the server name or auth_type  be found with 'auto configuration test'
        )
        # Sender accounts (send emails)
        self.sender_account = Account(
            primary_smtp_address=self.mailbox_address,
            credentials=self.credentials,
            autodiscover=False,
            config=self.exchangelib_config,
            access_type=DELEGATE
        )

        # Setup correction folder and done folder
        self.routing_folder_path = routing_folder_path
        self.correction_folder_path = correction_folder_path
        self.done_folder_path = done_folder_path

        logger.info(
            f"Connected to mailbox {self.mailbox_address} as user {self.login_address}"
        )

Usually with this new code the connexion has to be done. But it might be possible that a SSL certificate error pops up. To counter that you have to disable http verification like that :

from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter

BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

Now the connexion works !

Python version : 3.8

Melusine version : 2.3.1

Operating System : Windows 10