Dart
Dart
Determine the Number of Unseen Email Messages
Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final imap = CkImap();
// Connect to an IMAP server.
// Use TLS
imap.ssl = true;
imap.port = 993;
try {
imap.connect('imap.example.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Login
try {
imap.login('***', '***');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
imap.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// After selecting the mailbox. the total number of emails
// is immediately available:
final totalNum = imap.numMessages;
print('Num messages = $totalNum');
// To determine the number of unseen messages, a call
// to Search is required, which returns the set of UIDs
// of all unseen messages.
// We can choose to fetch UIDs or sequence numbers.
final fetchUids = true;
final messageSet = CkMessageSet();
try {
imap.queryMbx('UNSEEN', fetchUids, messageSet);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final numUnseen = messageSet.count;
print(numUnseen);
print('UIDs ----');
// Display the UIDs
var i = 0;
while (i < messageSet.count) {
print(messageSet.getId(i));
i++;
}
// Disconnect from the IMAP server.
imap.disconnect();
}