""" IMAP接続診断スクリプト 使い方: python test_imap.py """ import imaplib import ssl import getpass HOST = "outlook.office365.com" PORT = 993 USER = "akiracraftworl@infoseek.jp" print(f"IMAP診断: {HOST}:{PORT}") print(f"ユーザー: {USER}") print() # --- Step 1: SSL接続テスト(認証なし)--- print("[1] SSL接続テスト...") try: ssl_ctx = ssl.create_default_context() mail = imaplib.IMAP4_SSL(HOST, PORT, ssl_context=ssl_ctx) print(" ✓ SSL接続成功") except Exception as e: print(f" ❌ SSL接続失敗: {e}") exit(1) # --- Step 2: サーバーの認証方式を確認 --- print("[2] サーバー対応認証方式を確認...") try: typ, caps_data = mail.capability() caps = caps_data[0].decode() if caps_data and caps_data[0] else "" print(f" CAPABILITY: {caps}") if "AUTH=PLAIN" in caps or "AUTH=LOGIN" in caps: print(" ✓ 基本認証(パスワード)が使えます") basic_auth_supported = True else: basic_auth_supported = False if "AUTH=XOAUTH2" in caps or "AUTH=OAUTHBEARER" in caps: print(" ⚠ モダン認証(OAuth2)が必要な可能性があります") if not basic_auth_supported: print(" ❌ 基本認証は対応していません → OAuth2が必要です") mail.logout() exit(1) except Exception as e: print(f" CAPABILITY取得エラー: {e}") # --- Step 3: ログインテスト --- print("[3] ログインテスト...") password = getpass.getpass(" パスワードを入力: ") try: mail.login(USER, password) print(" ✓ ログイン成功!") mail.select("INBOX") _, data = mail.uid("SEARCH", None, "ALL") uids = data[0].split() if data[0] else [] print(f" ✓ INBOX: {len(uids)}件") mail.logout() print() print("✅ 成功!Windmillに登録できます。") except imaplib.IMAP4.error as e: err = str(e) print(f" ❌ ログイン失敗: {err}") print() if "disabled" in err.lower() or "imap" in err.lower(): print(" 原因: IMAPが無効化されています") print(" 対処: https://outlook.live.com → 設定 → メールの同期 → IMAPを有効化") elif "AUTHENTICATE" in err or "XOAUTH" in err: print(" 原因: モダン認証(OAuth2)が必要です") else: print(" 原因1: パスワードが間違っている") print(" 原因2: IMAPが無効(Outlook.com設定を再確認)") print(" 原因3: モダン認証が必要") print() print(" 確認してください:") print(" → https://outlook.live.com にこのメアドとパスワードでログインできますか?") except Exception as e: print(f" ❌ エラー: {type(e).__name__}: {e}")