サーバーからのメッセージ操作
メールボックス情報の取得
メッセージ数やメールボックスサイズなどの情報は、以下を使用して取得できます getMailBoxSize および getMailBoxInfo のメソッド Pop3Client クラス。
- この getMailBoxSize メソッドはメールボックスのサイズ(バイト単位)を返します。
- この getMailBoxInfo メソッドは型のオブジェクトを返します Pop3MailBoxInfo.
メッセージ数を取得するには、以下を使用することもできます MessageCount プロパティとサイズを使用して OccupiedSize プロパティ( Pop3MailBoxInfo クラス。以下のサンプルコードは、メールボックスの情報を取得する方法を示しています。次のことができます:
- 作成する Pop3Client.
- POP3サーバーに接続する。
- メールボックスのサイズを取得する。
- メールボックス情報を取得する。
- メールボックス内のメッセージ数を取得する。
- 使用中サイズを取得する。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
// Get the size of the mailbox, Get mailbox info, number of messages in the mailbox
long nSize = client.getMailboxSize();
System.out.println("Mailbox size is " + nSize + " bytes.");
Pop3MailboxInfo info = client.getMailboxInfo();
int nMessageCount = info.getMessageCount();
System.out.println("Number of messages in mailbox are " + nMessageCount);
long nOccupiedSize = info.getOccupiedSize();
System.out.println("Occupied size is " + nOccupiedSize);
メールボックス内のメール数取得
以下のコードスニペットは、メールボックス内のメールメッセージ数をカウントする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
Pop3Client client = new Pop3Client("pop3.server.com", "username", "password");
try {
client.setSecurityOptions(SecurityOptions.Auto);
int i = client.getMessageCount();
System.out.println("Message count: " + i);
} catch (Pop3Exception ex) {
System.out.println("Error:" + ex.toString());
}
Aspose.Email を使用すると、開発者はさまざまな方法でメールを操作できます。たとえば、メールをダウンロードする前にヘッダー情報を取得したり、メールをサーバーから取得して解析せずに保存したり(高速)、解析してから保存したり(低速)できます。本稿では、メールの取得と変換方法を示します。
メールヘッダー情報の取得
メールヘッダーはメールメッセージに関する情報を提供し、全体のメールメッセージを取得するかどうかの判断に利用できます。通常、ヘッダー情報には送信者、件名、受信日付などが含まれます(メールヘッダーの詳細は以下で説明しています)。 メールヘッダーのカスタマイズ. そのトピックは主にSMTPでメールを送信することについてですが、メールヘッダーの内容情報はPOP3メールでも有効です)。以下の例は、メッセージシーケンス番号でPOP3サーバーからメールヘッダーを取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username. password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
HeaderCollection headers = client.getMessageHeaders(1);
for (int i = 0; i < headers.size(); i++) {
// Display key and value in the header collection
System.out.print(headers.getKey(i));
System.out.print(" : ");
System.out.println(headers.get(i));
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
client.dispose();
}
メールメッセージの取得
この Pop3Client クラスコンポーネントは、POP3サーバーからメールメッセージを取得し、…に解析する機能を提供します MailMessage インスタンスは…の助けを借りて MailMessage コンポーネント。 MailMessage クラスは、メールコンテンツを操作するための複数のプロパティとメソッドを含みます。これらを使用して fetchMessage メソッド( Pop3Client クラスでは、次を取得できます。 MailMessage インスタンスを POP3 サーバーから直接取得します。以下のコードスニペットは、POP3 サーバーから完全なメールメッセージを取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
int messageCount = client.getMessageCount();
// Create an instance of the MailMessage class and Retrieve message
MailMessage message;
for (int i = 1; i <= messageCount; i++) {
message = client.fetchMessage(i);
System.out.println("From:" + message.getFrom());
System.out.println("Subject:" + message.getSubject());
System.out.println(message.getHtmlBody());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
client.dispose();
}
一意の ID を使用したメッセージ概要情報の取得
API の POP3 クライアントは、メッセージの一意の ID を使用してサーバーからメッセージの概要情報を取得できます。これにより、サーバーから完全なメッセージを取得せずに、メッセージの簡易情報にすばやくアクセスできます。以下のコードスニペットは、メッセージ概要情報を取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String uniqueId = "unique id of a message from server";
Pop3Client client = new Pop3Client("host.domain.com", 456, "username", "password");
client.setSecurityOptions(SecurityOptions.Auto);
Pop3MessageInfo messageInfo = client.getMessageInfo(uniqueId);
if (messageInfo != null) {
System.out.println(messageInfo.getSubject());
System.out.println(messageInfo.getDate());
}
マルチコネクションでメッセージ一覧取得
Pop3Client を提供します UseMultiConnection 負荷の高い操作のために複数の接続を作成できるプロパティです。また、マルチコネクションモード中に使用する接続数は次を使用して設定できます Pop3Client.ConnectionsQuantity以下のコードスニペットは、メッセージ一覧取得にマルチコネクションモードを使用する例と、シングルコネクションモードとのパフォーマンス比較を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client pop3Client = new Pop3Client();
pop3Client.setHost("<HOST>");
pop3Client.setPort(995);
pop3Client.setUsername("<USERNAME>");
pop3Client.setPassword("<PASSWORD>");
pop3Client.setConnectionsQuantity(5);
pop3Client.setUseMultiConnection(MultiConnectionMode.Enable);
long multiConnectionModeStartTime = System.currentTimeMillis();
Pop3MessageInfoCollection messageInfoCol1 = pop3Client.listMessages();
long multiConnectionModeTimeSpan = System.currentTimeMillis() - multiConnectionModeStartTime;
pop3Client.setUseMultiConnection(MultiConnectionMode.Disable);
long singleConnectionModeStartTime = System.currentTimeMillis();
Pop3MessageInfoCollection messageInfoCol2 = pop3Client.listMessages();
long singleConnectionModeTimeSpan = System.currentTimeMillis() - singleConnectionModeStartTime;
System.out.println("multyConnectionModeTimeSpan: " + multiConnectionModeTimeSpan);
System.out.println("singleConnectionModeTimeSpan: " + singleConnectionModeTimeSpan);
double performanceRelation = singleConnectionModeTimeSpan / multiConnectionModeTimeSpan;
System.out.println("Performance Relation: " + performanceRelation);
サーバーからメッセージを取得し、ディスクに保存
解析せずにメッセージをディスクに保存
解析せずにPOP3サーバーからメールメッセージをダウンロードしたい場合は、… を使用してください Pop3Client クラス saveMessage 関数。 saveMessage 関数はメールメッセージを解析しないため、… より高速です。 fetchMessage 関数。以下のコードスニペットは、シーケンス番号(この例では 1)でメッセージを保存する方法を示しています。 saveMessage メッセージを解析せずに元のEML形式で保存するメソッドです。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "pop3/";
String dstEmail = dataDir + "InsertHeaders.eml";
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
// Save message to disk by message sequence number
client.saveMessage(1, dstEmail);
client.dispose();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("Retrieved email messages using POP3 ");
保存前にメッセージを解析
以下のコードスニペットは、… を使用しています Pop3Client fetchMessage シーケンス番号でPOP3サーバーからメッセージを取得し、件名をファイル名としてディスクに保存するメソッドです。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "pop3/";
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();
// Specify host, username and password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
// Fetch the message by its sequence number and Save the message using its subject as the file name
MailMessage msg = client.fetchMessage(1);
msg.save(dataDir + "first-message_out.eml", SaveOptions.getDefaultEml());
client.dispose();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
client.dispose();
}
グループ取得メッセージ
Pop3Client を提供します fetchMessages シーケンス番号またはユニークIDのイテラブルを受け取り、… のリストを返すメソッドです MailMessage。以下のコードスニペットは、… の使用例を示しています fetchMessages シーケンス番号とユニークIDでメッセージを取得するメソッドです。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client pop3Client = new Pop3Client();
pop3Client.setHost("<HOST>");
pop3Client.setPort(995);
pop3Client.setUsername("<USERNAME>");
pop3Client.setPassword("<PASSWORD>");
Pop3MessageInfoCollection messageInfoCol = pop3Client.listMessages();
System.out.println("ListMessages Count: " + messageInfoCol.size());
List<Integer> sequenceNumberAr = new ArrayList<Integer>();
List<String> uniqueIdAr = new ArrayList<String>();
for (Pop3MessageInfo mi : messageInfoCol) {
sequenceNumberAr.add(mi.getSequenceNumber());
uniqueIdAr.add(mi.getUniqueId());
}
for (MailMessage m : pop3Client.fetchMessagesBySequences(sequenceNumberAr)) {
System.out.println("FetchMessages-sequenceNumberAr : " + m.getSubject());
}
for (MailMessage m : pop3Client.fetchMessagesByUids(uniqueIdAr)) {
System.out.println("FetchMessages-uniqueIdAr : " + m.getSubject());
}
送信者、受信者、または日付でのメッセージフィルタリング
この Pop3Client クラスは、以下で説明されています POP3 サーバーに接続、は listMessages メールボックスからすべてのメッセージを取得するメソッドです。条件に合致するメッセージだけを取得するには、オーバーロードされた listMessages を受け取るメソッド MailQuery を引数として。 MailQuery クラスは、日付、件名、送信者、受信者など、クエリ条件を指定するためのさまざまなプロパティを提供します。 MailQueryBuilder クラスは検索式を構築するために使用されます。まず、すべての条件と制約を設定し、次に MailQuery は、…によって作成されたクエリで満たされます MailQueryBuilder。 MailQuery クラスオブジェクトは…によって使用されます Pop3Client サーバーからフィルタリングされた情報を抽出します。本稿ではメールボックスからメールメッセージをフィルタリングする方法を示します。最初の例は、日付と件名に基づいてメッセージをフィルタリングする方法を示しています。また、他の条件でフィルタリングする方法や、より複雑なクエリを構築する方法も示します。さらに、日時フィルタを使用して特定のメールを取得する方法、大小文字を区別したフィルタリングの適用方法も示します。
メールボックスからのメッセージフィルタリング
メールボックスからメッセージをフィルタリングするには:
- POP3サーバーに接続してログインする.
- インスタンスを作成する MailQuery そして、必要なプロパティを設定します。
- 呼び出す Pop3Client.listMessages(MailQuery query) メソッドに渡す MailQuery パラメータでフィルタリングされたメッセージのみを取得します。
以下のコードスニペットは、POP3メールボックスに接続し、本日受信した件名に "newsletter" を含むメッセージを取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Connect and log in to POP3
String host = "host";
int port = 110;
String username = "user@host.com";
String password = "password";
Pop3Client client = new Pop3Client(host, port, username, password);
// Set conditions, Subject contains "Newsletter", Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.getSubject().contains("Newsletter");
builder.getInternalDate().on(new Date());
// Build the query and Get list of messages
MailQuery query = builder.getQuery();
Pop3MessageInfoCollection messages = client.listMessages(query);
System.out.println("Pop3: " + messages.size() + " message(s) found.");
特定の条件に合致するメッセージの取得
上記のコードサンプル メールの件名と日付に基づいてメッセージをフィルタリングします。他のプロパティを使用して、サポートされている他の条件も設定できます。以下は、… を使用して条件を設定する例です。 MailQuery.
以下のコードスニペットは、他の条件でメールをフィルタリングする方法を示しています:
- 本日配信されたメールを検索する。
- 一定期間内に受信したメールを検索する。
- 特定の送信者からのメールを検索する。
- 特定のドメインから送信されたメールを検索する。
- 特定の受信者に送信されたメールを検索する。
本日
以下のコードスニペットは、本日配信されたメールを検索する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.getInternalDate().on(new Date());
日付範囲
以下のコードスニペットは、一定期間内に受信したメールを検索する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails that arrived in last 7 days
Calendar calendar = Calendar.getInstance();
builder.getInternalDate().before(calendar.getTime());
calendar.add(Calendar.DATE, -7);
builder.getInternalDate().since(calendar.getTime());
特定の送信者
以下のコードスニペットは、特定の送信者からのメールを検索する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails from specific sender
builder.getFrom().contains("saqib.razzaq@127.0.0.1");
特定のドメイン
以下のコードスニペットは、特定のドメインから送信されたメールを検索する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails from specific domain
builder.getFrom().contains("SpecificHost.com");
特定の受信者
以下のコードスニペットは、特定の受信者に送信されたメールを検索する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails sent to specific recipient
builder.getTo().contains("recipient");
複雑なクエリの構築
異なる場合 MailQueryBuilder プロパティを別々のステートメントで設定すると、すべての条件が一致します。たとえば、日付範囲内かつ特定のホストからのメッセージを取得したい場合、3 つのステートメントを書く必要があります。
AND を使用したクエリの組み合わせ
以下のコードスニペットは、AND を使用してクエリを組み合わせる方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago
builder.getFrom().contains("SpecificHost.com");
Calendar calendar = Calendar.getInstance();
builder.getInternalDate().before(calendar.getTime());
calendar.add(Calendar.DATE, -7);
builder.getInternalDate().since(calendar.getTime());
OR を使用したクエリの組み合わせ
MailQueryBuilder 提供します または 2つを受け取るメソッド MailQuery インスタンスをパラメータとして渡します。指定された 2 つの条件のいずれかに一致するメッセージを取得します。以下のコードスニペットは、件名に "test" が含まれるか、送信者が "noreply@host.com" のメッセージをフィルタリングする方法を示しています。また、OR を使用してクエリを組み合わせる方法も示しています。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Specify OR condition
builder.or(builder.getSubject().contains("test"), builder.getFrom().contains("noreply@host.com"));
ケースセンシティブフィルタの適用
API は、大小文字を区別する条件に基づいてメールボックスからメールをフィルタリングする機能も提供します。以下のメソッドは、大小文字フラグを指定してメールを検索する機能を提供します。
- Method StringComparisonField.contains(String value, boolean ignoreCase)
- Method StringComparisonField.equals(String value, boolean ignoreCase)
- Method StringComparisonField.notContains(String boolean, bool ignoreCase)
- Method StringComparisonField.notEquals(String boolean, bool ignoreCase)
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// IgnoreCase is True
MailQueryBuilder builder1 = new MailQueryBuilder();
builder1.getFrom().contains("tesT", true);
MailQuery query1 = builder1.getQuery();
Pop3MessageInfoCollection messageInfoCol1 = client.listMessages(query1);