import imaplib import os import ssl IMAP_SERVER = "outlook.office365.com" IMAP_PORT = 993 O365_IMAP_USERNAME="i.meszely@aps-hh.de" O365_IMAP_PASSWORD="virgI6774#" def test_imap_connection(username, password): try: # Create a default SSL context ssl_context = ssl.create_default_context() # Connect to the IMAP server over SSL print(f"Connecting to IMAP server: {IMAP_SERVER}:{IMAP_PORT}...") with imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT, ssl_context=ssl_context) as mail: print("Connection established. Attempting to log in...") mail.login(username, password) print(f"Successfully logged in as {username}.") mail.logout() print("Logged out.") return True except imaplib.IMAP4.error as e: print(f"IMAP login failed for {username}: {e}") return False except Exception as e: print(f"An unexpected error occurred: {e}") return False if __name__ == "__main__": # WARNING: Hardcoding credentials is not recommended for security reasons. # Consider using environment variables or a secure configuration method. username = O365_IMAP_USERNAME password = O365_IMAP_PASSWORD print(f"Testing IMAP connection for user: {username}") if test_imap_connection(username, password): print("\nIMAP connection test completed successfully!") else: print("\nIMAP connection test failed.")