Dart Requires Chilkat v11.0.0+
Dart
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another 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 imapSrc = CkImap();
// Connect to our source IMAP server.
try {
imapSrc.connect('imap.example.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Login to the source IMAP server
try {
imapSrc.login('admin@chilkatsoft.com', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final imapDest = CkImap();
// Connect to our destination IMAP server.
try {
imapDest.connect('mail.example-code.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Login to the destination IMAP server
try {
imapDest.login('myLogin', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Select an IMAP mailbox on the source IMAP server
try {
imapSrc.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
print(imapSrc.numMessages);
// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.
// This example will copy the first 10 messages using sequence numbers
final sbMime = CkStringBuilder();
for (var seqNum = 1; seqNum <= 10; seqNum++) {
sbMime.clear();
try {
imapSrc.fetchSingleAsMimeSb(seqNum, false, sbMime);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imapDest.appendMimeWithFlagsSb('Inbox', sbMime, false, false, false, false);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
}
// Disconnect from the IMAP servers.
imapSrc.disconnect();
imapDest.disconnect();
}