Dart
Dart
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
// MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
// is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
final imap = CkImap();
imap.ssl = true;
imap.port = 993;
try {
imap.connect('imap.example.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imap.login('user@example.com', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imap.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Find the messages to download (here, the unseen messages by UID).
final msgSet = CkMessageSet();
try {
imap.queryMbx('UNSEEN', true, msgSet);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Download the messages in the set (full bodies) into a bundle.
final bundle = CkEmailBundle();
try {
imap.fetchMsgSet(false, msgSet, bundle);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final n = bundle.messageCount;
print('Fetched $n messages.');
final email = CkEmail();
for (var i = 0; i < n; i++) {
bundle.emailAt(i, email);
print(email.subject);
}
try {
imap.disconnect();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
}