You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用IMAP登录Outlook Hotmail需多次尝试(LOGIN失败)才能成功

Hotmail/Outlook IMAP多次登录失败后才成功的问题排查

问题说明

已为Hotmail/Outlook账户启用应用密码,使用IMAP协议连接outlook.office365.com时,前5-10次登录尝试均返回LOGIN failed错误,多次重试后才能成功登录。连接服务器始终成功,但登录验证反复失败。

可能原因

  • Office 365临时限流/风控:微软服务器针对短时间内频繁的登录请求会触发临时限制,尤其是每次重试都新建连接的情况,这类限制是暂时性的,多次尝试后会自动解除。
  • 连接复用缺失:当前代码每次重试都新建IMAP连接,频繁的新连接请求容易被服务器识别为自动化操作,触发防御机制。
  • 应用密码同步延迟:如果是刚生成的应用密码,微软服务器可能存在同步延迟,导致初期验证不通过。

解决办法

1. 优化连接复用逻辑

不要每次重试都新建连接,而是在同一个已建立的连接上多次尝试登录,减少服务器端的连接压力,降低被限流的概率。修改后的代码如下:

import logging
import imaplib
import sys
import time  # 补充原代码缺失的time模块

# 账户信息
email_user = "user1@hotmail.com"  
email_pass = "APPPASSWORD"      
IMAPserver = "outlook.office365.com"
ImapPort = 993

# 日志配置
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log_handler_console = logging.StreamHandler(sys.stdout)
log_handler_console.setFormatter(log_formatter)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(log_handler_console)

def connect_to_server(server, port):
    try:
        mail = imaplib.IMAP4_SSL(server, port)
        logging.info(f"已连接到邮件服务器 {server}.")
        return mail
    except imaplib.IMAP4.error as e:
        logging.error(f"连接失败: {str(e)}")
        return None
        
def login_with_retries(server, port, email_user, email_pass, max_retries=30, retry_delay=5):
    # 先建立一次连接,后续重试复用该连接
    mail = connect_to_server(server, port)
    if mail is None:
        logging.error("初始连接建立失败.")
        return None
        
    for attempt in range(max_retries):
        try:
            mail.login(email_user, email_pass)
            logging.info(f"成功登录邮箱账户 {email_user}.")
            return mail
        except imaplib.IMAP4.error as e:
            logging.error(f"第 {attempt + 1} 次登录失败({max_retries}次重试上限),{retry_delay}秒后重试. 错误信息: {str(e)}")
            time.sleep(retry_delay)
    
    # 所有尝试失败后关闭连接
    try:
        mail.logout()
    except:
        pass
    logging.error("所有登录尝试均失败,请检查账户凭据和设置.")
    return None

try: 
    mail = login_with_retries(IMAPserver, ImapPort, email_user, email_pass)
    
    if mail is None:
        raise RuntimeError("多次尝试后仍无法完成IMAP登录.")

    # 选择收件箱
    mail.select("inbox")
    logging.info("已选中收件箱文件夹.")

except imaplib.IMAP4.error as e:
    logging.error(f"IMAP错误发生: {e}", exc_info=True)
    time.sleep(10)
    sys.exit(1)

except Exception as e:
    logging.error(f"意外错误发生: {e}", exc_info=True)
    time.sleep(10)
    sys.exit(1)

2. 增加重试间隔的随机性

固定间隔重试容易被识别为自动化请求,可将重试间隔改为随机值,比如3-8秒:

import random
# 在重试时替换time.sleep(retry_delay)为:
time.sleep(random.randint(3, 8))

3. 检查账户安全状态

登录微软账户中心,查看是否有异常登录记录,确认是否开启了额外的安全验证规则,确保应用密码的权限配置正确。


原始代码

import logging
import imaplib
import sys

# Your email credentials
email_user = "user1@hotmail.com"  
email_pass = "APPPASSWORD"      
IMAPserver = "outlook.office365.com"
ImapPort = 993

# Configure logging to write to both file and console
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

# Console handler
log_handler_console = logging.StreamHandler(sys.stdout)
log_handler_console.setFormatter(log_formatter)

# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(log_handler_console)

def connect_to_server(server, port):
    try:
        mail = imaplib.IMAP4_SSL(server, port)
        logging.info(f"Connected to the email server {server}.")
        return mail
    except imaplib.IMAP4.error as e:
        logging.error(f"Connection failed: {str(e)}")
        return None
        
def login_with_retries(server, port, email_user, email_pass, max_retries=30, retry_delay=5):
    for attempt in range(max_retries):
        mail = connect_to_server(server, port)
        if mail is None:
            logging.error(f"Retrying connection in {retry_delay} seconds...")
            time.sleep(retry_delay)
            continue
        try:
            
            mail.login(email_user, email_pass)
            logging.info(f"Logged into the email account {email_user}.")
            return mail  # Return the mail object if login is successful
        except imaplib.IMAP4.error as e:
            logging.error(f"LOGIN failed on attempt {attempt + 1} of {max_retries} (Retry in {retry_delay} seconds). Error: {str(e)}")
            mail.logout()  # Ensure proper logout before retrying connection
            time.sleep(retry_delay)
    logging.error("All login attempts failed. Check your email credentials and settings.")
    return None  # Return False if all attempts fail

try: 
    # Connect to server
    mail = login_with_retries(IMAPserver, ImapPort, email_user, email_pass)
    
    if mail is None:
        # Handle login failure
        raise RuntimeError("Failed to IMAP login after multiple attempts.")

    # Select mailbox
    mail.select("inbox")
    logging.info("Mailbox inbox folder selected.")

except imaplib.IMAP4.error as e:
    #print(f"An IMAP error occurred: {e}")
    logging.error(f"An IMAP error occurred: {e}", exc_info=True)
    wait_with_timeout(sleepError)
    sys.exit(1)

except Exception as e:
    #print(f"An unexpected error occurred: {e}")
    logging.error(f"An unexpected error occurred: {e}", exc_info=True)
    wait_with_timeout(sleepError)
    sys.exit(1)

运行日志

2024-08-01 14:04:49 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:04:51 - ERROR - LOGIN failed on attempt 1 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:04:56 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:04:57 - ERROR - LOGIN failed on attempt 2 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:02 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:03 - ERROR - LOGIN failed on attempt 3 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:09 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:10 - ERROR - LOGIN failed on attempt 4 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:15 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:16 - ERROR - LOGIN failed on attempt 5 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:21 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:23 - ERROR - LOGIN failed on attempt 6 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:28 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:29 - ERROR - LOGIN failed on attempt 7 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:35 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:36 - ERROR - LOGIN failed on attempt 8 of 30 (Retry in 5 seconds). Error: b'LOGIN failed.'
2024-08-01 14:05:41 - INFO - Connected to the email server outlook.office365.com.
2024-08-01 14:05:42 - INFO - Logged into the email account user1@hotmail.com.
2024-08-01 14:05:44 - INFO - Mailbox inbox folder selected.

内容的提问来源于stack exchange,提问作者Bharat Goyal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 01:55:56