Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat Dart Downloads

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

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final mail = CkMailMan();
  mail.mailHost = 'pop.example.com';
  mail.mailPort = 995;
  mail.popSsl = true;
  mail.requireSslCertVerify = true;
  mail.popUsername = 'MAIL_USER';
  mail.popPassword = 'MAIL_PASSWORD';
  final previews = CkEmailBundle();
  // Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
  try {
    mail.fetchRange(true, true, 0, 0, 4, previews);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if (previews.messageCount == 0) {
    print('There are no messages to preview.');
    try {
      mail.pop3EndSession();
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    return;
  }

  final header = CkEmail();
  var i = 0;
  while (i < previews.messageCount) {
    try {
      previews.emailAt(i, header);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    print('${header.uidl}  ${header.subject}');
    i++;
  }

  // A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
  try {
    previews.emailAt(0, header);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final email = CkEmail();
  try {
    mail.fetchFull(header, email);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    mail.pop3EndSession();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Full message: ${email.subject}  Files: ${email.numAttachments}');

  try {
    email.saveEml('pop-received.eml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Saved pop-received.eml.');
}