在 Python 中连接 IMAP 服务器

Aspose.Email for Python via .NET 包含 ImapClient 类,允许开发者使用 IMAP(Internet Message Access Protocol)连接邮件服务器。该类在 Python 应用程序中实现安全高效的电子邮件管理,包括检索、读取、移动、删除和更新邮件。

使用 IMAP 客户端,您可以对用户进行身份验证、管理邮箱文件夹、通过 SSL 连接、设置自定义超时,并通过代理服务器或 CRAM-MD5 身份验证访问电子邮件账户。

基础 IMAP 连接

要使用 Aspose.Email 连接到 IMAP 服务器,请遵循以下三个简单步骤:

  1. 创建该类的实例 ImapClient 类。
  2. 指定主机名、端口、用户名和密码。
  3. 指定所需的安全选项。

下面的代码示例展示了如何以编程方式连接到 IMAP 服务器:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd")

为 IMAP 连接启用 SSL

要连接到启用了 SSL 的 IMAP 服务器,请设置 security_options 属性用于 SSLImplicit:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd")

# Set the security mode to implicit
client.security_options = ae.clients.SecurityOptions.SSL_EXPLICIT

通过 SOCKS 代理连接

Aspose.Email 支持 SOCKS 4、4a 和 5 版本的代理连接。请按照以下步骤通过 SOCKS 代理进行连接:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", "username", "password")
client.security_options = ae.clients.SecurityOptions.AUTO

proxy = ae.clients.SocksProxy("192.168.203.142", 1080, ae.clients.SocksVersion.SOCKS_V5)
client.proxy = proxy

client.select_folder("Inbox")

通过 HTTP 代理连接服务器

Aspose.Email 还支持通过 HTTP 代理进行 IMAP 连接:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", "username", "password")
client.proxy = ae.clients.HttpProxy("18.222.124.59", 8080)
client.select_folder("Inbox")

只读邮箱访问

为防止对邮箱内容的更改,请启用只读模式。 read_only 属性设置为 True 表示不允许进行更改。下面的代码示例展示了如何在项目中使用此属性:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd")
client.security_options = ae.clients.SecurityOptions.SSL_EXPLICIT
client.read_only = True

使用 CRAM-MD5 身份验证

为提升安全性,请配置客户端使用 CRAM-MD5 身份验证:

下面的代码示例演示了如何配置客户端,以接受 CRAM-MD5 作为连接 IMAP 服务器的支持认证方法之一:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd")
client.allowed_authentication = ae.clients.imap.ImapKnownAuthenticationType.CRAM_MD5

设置操作超时

通过设置超时(以毫秒为单位),防止客户端无限期等待:

下面的代码片段展示了如何将客户端等待服务器响应的超时时间设置为 60,000 毫秒(60 秒):

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd", ae.clients.SecurityOptions.SSL_IMPLICIT)
#  60 seconds
client.timeout = 60000

设置问候超时

控制客户端在与邮件服务器初始握手期间等待的时间。

下面的代码片段展示了如何使用 greeting_timeout 属性限制问候超时 ImapClient 类:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd")

client.greeting_timeout = 4000
client.select_folder(ae.clients.imap.ImapFolderInfo.IN_BOX)

在 IMAP 客户端中使用 TLS

Aspose.Email 支持 TLS 和 SSL 进行安全通信。使用 supported_encryption 属性用于定义可接受的协议版本。

注意: 您只能设置 .NET Framework 支持的协议版本。如果您当前的 .NET Framework 版本不支持某些协议版本,这些版本将被忽略并跳过,这可能导致 TLS 安全级别降级。在这种情况下,不会生成异常。如果您想在不进行兼容性检查的情况下设置协议,请使用 ‘set_supported_encryption_unsafe(value)’ 方法。

下面的代码示例展示了如何为 ImapClient 类实例。

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd", ae.clients.SecurityOptions.SSL_IMPLICIT)

client.supported_encryption = ae.clients.base.EncryptionProtocols.TLS13

如果在当前 .NET Framework 版本中不支持指定的加密协议,set_supported_encryption_unsafe(value) 方法与 supported_encryption 属性的行为差异如下:

  • 如果使用 supported_encryption 属性,邮件客户端会将加密协议降级到受支持的级别。
  • 如果使用 set_supported_encryption_unsafe(value) 方法,邮件客户端会抛出异常。

验证 IMAP 服务器凭据

在执行后续操作之前建立到 IMAP 服务器的安全连接,需要检查并验证用户凭据。validate_credentials 方法属于 ImapClient 类用于检查提供的用户名和密码是否正确。如果凭据有效,客户端即可成功向 IMAP 服务器进行身份验证。下面的代码示例展示了如何在项目中实现此方法:

import aspose.email as ae

with ae.clients.imap.ImapClient("your imap server", 993, "your username", "your password", ae.clients.SecurityOptions.AUTO) as client:
    client.timeout = 4000

    if client.validate_credentials():
        # Further actions

启用 IMAP 活动日志记录

活动日志记录包括服务器连接、发送和接收邮件的传输详情、电子邮件处理过程中的错误信息以及客户端或服务器执行的其他操作。要跟踪 IMAP 客户端活动及其与服务器的交互,请使用下面的代码示例,该示例使用 log_file_name 属性的 ImapClient 类,用于将信息记录到指定的日志文件:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd", ae.clients.SecurityOptions.SSL_IMPLICIT)

# Set the path to the log file using the LogFileName property.
client.log_file_name = "C:\\Aspose.Email.IMAP.log"
# Set the UseDateInLogFileName property if it is necessary.
client.use_date_in_log_file_name = False