连接到 IMAP 服务器
该 ImapClient 该类允许应用程序使用 IMAP 协议管理 IMAP 邮箱。该 ImapClient 该类用于连接 IMAP 邮件服务器并管理 IMAP 邮件文件夹中的邮件。要连接到 IMAP 服务器
- 创建该类的实例 ImapClient 类。
- 在构造函数中指定主机名、用户名和密码 ImapClient 构造函数.
一旦 ImapClient 实例已初始化,下一次使用此实例调用任何操作时将连接到服务器。以下代码片段展示了如何使用上述步骤连接到 IMAP 服务器。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an imapclient with host, user and password
ImapClient client = new ImapClient("localhost", "user", "password");
使用启用 SSL 的 IMAP 服务器连接
使用 IMAP 服务器连接 描述了如何通过四个简单步骤连接到 IMAP 服务器:
- 创建该类的实例 ImapClient 类。
- 指定主机名、用户名和密码。
- 指定端口。
- 指定 安全选项.
连接到启用 SSL 的 IMAP 服务器的过程类似,但需要您设置另外几个属性:
- 设置 安全选项 到 SSLImplicit。
以下代码片段展示了如何
- 设置用户名、密码和端口。
- 设置安全选项。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the ImapClient class
ImapClient client = new ImapClient("imap.domain.com", 993, "user@domain.com", "pwd");
// Set the security mode to implicit
client.setSecurityOptions(SecurityOptions.SSLImplicit);
通过代理连接到服务器
代理服务器通常用于与外部世界通信。在这种情况下,邮件客户端如果不指定代理地址将无法通过互联网进行通信。Aspose.Email 支持 SOCKS 代理协议的 4、4a 和 5 版本。本文提供了使用代理邮件服务器访问邮箱的工作示例。要通过代理服务器访问邮箱:
- 初始化 SocksProxy 使用所需信息,即代理地址、端口和 SOCKS 版本。
- 初始化 ImapClient 使用主机地址、用户名、密码以及其他设置。
- 设置客户端 SocksProxy 属性到 SocksProxy 上述创建的对象。
以下代码片段展示了如何通过代理服务器检索邮箱。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Connect and log in to IMAP and set SecurityOptions
ImapClient client = new ImapClient("imap.domain.com", "username", "password");
client.setSecurityOptions(SecurityOptions.Auto);
String proxyAddress = "192.168.203.142"; // proxy address
int proxyPort = 1080; // proxy port
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5);
// Set the proxy
client.setProxy(proxy);
try {
client.selectFolder("Inbox");
} catch (java.lang.RuntimeException ex) {
System.out.println(ex.getMessage());
}
通过 HTTP 代理连接服务器
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
final ImapClient client = new ImapClient("imap.domain.com", "username", "password");
try {
client.setProxy(proxy);
client.selectFolder("Inbox");
} finally {
if (client != null)
client.dispose();
}
自定义身份验证机制
使用 … 检索 IMAP 服务器支持的身份验证机制列表 getSupportedAuthentication 方法的 ImapClient 类。此方法允许客户端确定哪些身份验证方法可用于与服务器建立安全连接。然后,使用 setAllowedAuthentication 方法用于获取(或设置)用户允许的身份验证类型枚举,选择最合适的客户端-服务器通信身份验证机制。这允许您明确设置邮件客户端的身份验证方式。
以下代码示例展示了如何自定义电子邮件客户端的身份验证:
imapClient.setAllowedAuthentication(ImapKnownAuthenticationType.Plain);
以只读模式连接服务器
该 ImapClient 类提供了一个 ReadOnly 属性,将其设置为 true 时,表示不应对邮箱的永久状态进行任何更改。以下代码示例演示了如何使用 ImapClient.ReadOnly 属性。它获取未读邮件的计数,然后获取一封邮件,再在只读模式下再次获取未读邮件计数。未读邮件计数保持不变,表明邮箱的永久状态未被更改。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient imapClient = new ImapClient();
imapClient.setHost("<HOST>");
imapClient.setPort(993);
imapClient.setUsername("<USERNAME>");
imapClient.setPassword("<PASSWORD>");
imapClient.setSupportedEncryption(EncryptionProtocols.Tls);
imapClient.setSecurityOptions(SecurityOptions.SSLImplicit);
ImapQueryBuilder imapQueryBuilder = new ImapQueryBuilder();
imapQueryBuilder.hasNoFlags(ImapMessageFlags.isRead()); /* get unread messages. */
MailQuery query = imapQueryBuilder.getQuery();
imapClient.setReadOnly(true);
imapClient.selectFolder("Inbox");
ImapMessageInfoCollection messageInfoCol = imapClient.listMessages(query);
System.out.println("Initial Unread Count: " + messageInfoCol.size());
if (messageInfoCol.size() > 0) {
imapClient.fetchMessage(messageInfoCol.get_Item(0).getSequenceNumber());
messageInfoCol = imapClient.listMessages(query);
// This count will be equal to the initial count
System.out.println("Updated Unread Count: " + messageInfoCol.size());
} else {
System.out.println("No unread messages found");
}
使用 CRAM-MD5 身份验证连接到服务器
为确保与 IMAP 服务器的安全认证和通信,您可以指定并强制使用 CRAM-MD5 作为 IMAP 客户端允许的认证方式。以下代码片段展示了如何为 … 配置允许的认证类型。 ImapClient:
imapClient.setAllowedAuthentication(ImapKnownAuthenticationType.CramMD5);
在不发送电子邮件的情况下验证邮件服务器凭据
有时需要在不发送电子邮件的情况下验证凭据。Aspose.Email 提供了 validateCredentials() 方法用于执行此操作。如果验证成功,将执行 if 语句内的代码,通常用于执行进一步操作或从 IMAP 服务器检索数据。以下代码片段演示了在不发送电子邮件的情况下验证凭据:
try (ImapClient imapClient = new ImapClient(
server.ImapUrl, server.ImapPort, "username", "password", SecurityOptions.Auto)) {
imapClient.setTimeout(4000);
if (imapClient.validateCredentials()) {
// to do something
}
}
如何为邮件操作设置超时
每个邮件操作都需要一定时间,取决于多种因素(网络延迟、数据大小、服务器性能等)。您可以为所有邮件操作设置超时。下面的代码示例展示了如何使用 超时 属性。注意:不应设置过大的值,以免在应用程序中导致长时间等待。
try (ImapClient imapClient = new ImapClient("host", 993, "username", "password", SecurityOptions.SSLImplicit))
{
imapClient.setTimeout(60000); // 60 seconds
// some code...
}
如何限制问候超时
IMAP 客户端可以使用自动模式建立连接。在此模式下,IMAP 客户端会遍历所有可能的连接参数,直至成功建立连接。若连接正确,IMAP 服务器会向客户端发送问候字符串。服务器可能使用隐式或显式(START TLS)SSL/TLS 连接初始化。如果连接模式不匹配(例如,服务器等待隐式 SSL 连接,而客户端尝试建立非安全或显式 SSL 连接),服务器将不会发送问候字符串,用户将长时间等待直至超时后才收到问候字符串,客户端随后尝试下一个连接选项。为避免此问题,引入了 GreetingTimeout 属性。此属性允许您设置问候字符串的超时时间,从而缩短自动连接建立的时间。
ImapClient imapClient = new ImapClient();
imapClient.setGreetingTimeout(4000);