Sample code for 30+ languages & platforms
Dart

Create an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.CreateMailbox method, which creates a mailbox on the IMAP server. When creating nested mailboxes, use the server's hierarchy delimiter reported in the SeparatorChar property. This example creates a mailbox named Archive2026.

Background: IMAP folders form a hierarchy, and the character that separates levels varies by server — commonly / or .. To create a sub-folder you build the path with that delimiter (for example Archive/2026), which is why you should read SeparatorChar rather than assume one. Creating mailboxes lets an application organize mail into folders programmatically, such as filing messages by year or project.

Chilkat Dart Downloads

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

void main() {
  // Demonstrates the Imap.CreateMailbox method, which creates a mailbox on the IMAP server.
  // Use the server's hierarchy delimiter (the SeparatorChar property) when creating nested
  // mailboxes.

  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;
  }

  // Create a new mailbox (folder) on the server.
  try {
    imap.createMailbox('Archive2026');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Mailbox created.');

  try {
    imap.disconnect();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }
}