Sample code for 30+ languages & platforms
Dart

Copy Email from one IMAP Account to Another

See more IMAP Examples

Demonstrates how to copy the email in a mailbox from one account to another.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final imapSrc = CkImap();

  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Connect to our source IMAP server.
  imapSrc.ssl = true;
  imapSrc.port = 993;
  try {
    imapSrc.connect('MY-IMAP-DOMAIN');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Login to the source IMAP server
  try {
    imapSrc.login('MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final imapDest = CkImap();

  // Connect to our destination IMAP server.
  imapDest.ssl = true;
  imapDest.port = 993;
  try {
    imapDest.connect('MY-IMAP-DOMAIN2');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Login to the destination IMAP server
  try {
    imapDest.login('MY-IMAP-LOGIN2', 'MY-IMAP-PASSWORD2');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Select a source IMAP mailbox on the source IMAP server
  try {
    imapSrc.selectMailbox('Inbox');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final fetchUids = true;

  // Get the set of UIDs for all emails on the source server.
  final CkMessageSet mset;
  try {
    mset = imapSrc.search('ALL', fetchUids);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Load the complete set of UIDs that were previously copied.
  // We dont' want to copy any of these to the destination.
  final fac = CkFileAccess();
  final msetAlreadyCopied = CkMessageSet();
  var strMsgSet = fac.readEntireTextFile('qa_cache/saAlreadyLoaded.txt', 'utf-8');
  if (fac.lastMethodSuccess) {
    msetAlreadyCopied.fromCompactString(strMsgSet);
  }

  final numUids = mset.count;
  final sbFlags = CkStringBuilder();

  var i = 0;
  while (i < numUids) {

    // If this UID was not already copied...
    final uid = mset.getId(i);
    if (!msetAlreadyCopied.containsId(uid)) {

      print('copying $uid...');

      // Get the flags.
      final String flags;
      try {
        flags = imapSrc.fetchFlags(uid, true);
      } on ChilkatException catch (e) {
        print(e.lastErrorText);
        return;
      }

      sbFlags.setString(flags);

      // Get the MIME of this email from the source.
      final String mimeStr;
      try {
        mimeStr = imapSrc.fetchSingleAsMime(uid, true);
      } on ChilkatException catch (e) {
        print(e.lastErrorText);
        return;
      }

      final seen = sbFlags.contains('\\Seen', false);
      final flagged = sbFlags.contains('\\Flagged', false);
      final answered = sbFlags.contains('\\Answered', false);
      final draft = sbFlags.contains('\\Draft', false);

      try {
        imapDest.appendMimeWithFlags('Inbox', mimeStr, seen, flagged, answered, draft);
      } on ChilkatException catch (e) {
        print(e.lastErrorText);
        return;
      }

      // Update msetAlreadyCopied with the uid just copied.
      msetAlreadyCopied.insertId(uid);

      // Save at every iteration just in case there's a failure..
      strMsgSet = msetAlreadyCopied.toCompactString();
      fac.writeEntireTextFile('qa_cache/saAlreadyLoaded.txt', strMsgSet, 'utf-8', false);
    }

    i++;
  }

  // Disconnect from the IMAP servers.
  imapSrc.disconnect();
  imapDest.disconnect();
}