Sample code for 30+ languages & platforms
Dart

Add Email Attachment from FTP

Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.

Note: This example requires Chilkat v9.5.0.63 or later.

Chilkat Dart Downloads

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

void main() {
  var success = false;

  // Note: This example requires Chilkat v9.5.0.63 or later.

  // This example assumes Chilkat Ftp2 to have been previously unlocked.
  // See Unlock Ftp2 for sample code.

  final ftp = CkFtp2();

  ftp.hostname = 'www.my-ftp-server.com';
  ftp.username = 'mFtpLogin';
  ftp.password = 'myFtpPassword';

  // Connect to the FTP server.
  success = true;
  try {
    ftp.connectOnly();
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(ftp.lastErrorText);
    return;
  }

  // Authenticate with the FTP server.
  success = true;
  try {
    ftp.loginAfterConnectOnly();
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(ftp.lastErrorText);
    return;
  }

  // Move to the remove directory where our file is located.
  success = true;
  try {
    ftp.changeRemoteDir('qa_data');
  } on ChilkatException {
    success = false;
  }
  if (success) {
    success = true;
    try {
      ftp.changeRemoteDir('pdf');
    } on ChilkatException {
      success = false;
    }
  }

  if (!success) {
    print(ftp.lastErrorText);
    return;
  }

  // Download...
  final pdfData = CkBinData();
  success = true;
  try {
    ftp.getFileBd('fishing.pdf', pdfData);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(ftp.lastErrorText);
    return;
  }

  ftp.disconnect();

  // Create an email object, and add the PDF as an attachment.
  final email = CkEmail();
  email.subject = 'Test with PDF attachment.';
  email.body = 'This is a plain-text body..';

  // Add the PDF attachment.  (This method call requires Chilkat v9.5.0.63 or later)
  success = true;
  try {
    email.addAttachmentBd('fishing.pdf', pdfData, 'application/pdf');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(email.lastErrorText);
    return;
  }

  // Save the email and examine with a text editor to see the PDF attachment is present..
  email.saveEml('qa_output/out.eml');

  print('Success.');
}