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 サーバーへの接続方法を4つの簡単な手順で説明します:
- インスタンスを作成します 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 をサポートしています。本記事では、プロキシメールサーバーを使用してメールボックスにアクセスする動作サンプルを提供します。プロキシサーバー経由でメールボックスにアクセスするには:
- 初期化 Socksプロキシ 必要な情報(プロキシアドレス、ポート、SOCKS バージョン)と共に。
- 初期化 ImapClient ホストアドレス、ユーザー名、パスワード、その他の設定と共に。
- クライアントを設定する Socksプロキシ プロパティを Socksプロキシ 上記で作成したオブジェクト。
以下のコードスニペットは、プロキシサーバー経由でメールボックスを取得する方法を示します。
// 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 プロパティです。未読メッセージ数を取得し、1 通のメッセージを取得し、読み取り専用モードで再び未読メッセージ数を取得します。未読メッセージ数が変わらないことは、メールボックスの永続状態が変更されていないことを示します。
// 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 サーバーとの安全な認証と通信を確保するために、IMAP クライアントの許可された認証方法として CRAM-MD5 の使用を指定し、強制できます。以下のコードスニペットは、許可された認証タイプを設定する方法を示します。 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...
}
Greeting Timeout の制限方法
IMAP クライアントは自動モードを使用して接続を確立することがあります。このモードでは、IMAP クライアントは接続が確立するまで可能なすべての接続パラメータを試みます。正しい接続が確立すると、IMAP サーバーはクライアントに挨拶文字列を送信します。サーバーは暗黙的または明示的(START TLS)SSL/TLS 接続開始を使用できます。接続モードが一致しない場合(例: サーバーが暗黙的 SSL 接続を待っているが、クライアントが非保護または明示的 SSL 接続を確立しようとする場合)、サーバーは挨拶文字列を送信せず、タイムアウトが発生するまでユーザーは長時間待機し、クライアントは次の接続オプションへ進みます。この問題を回避するために GreetingTimeout が導入されました。このプロパティにより、挨拶文字列のタイムアウトを設定でき、自動接続確立の時間を短縮できます。
ImapClient imapClient = new ImapClient();
imapClient.setGreetingTimeout(4000);