Sample code for 30+ languages & platforms
React Native

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

Chilkat React Native Downloads

React Native
import { Email, Imap } from '@chilkat/react-native'

async function chilkatExample() {
  // Demonstrates the Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its
  // initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
  // 3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

  const imap = new Imap();

  imap.ssl = true;
  imap.port = 993;

  try {
    await imap.connectAsync('imap.example.com');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  try {
    await imap.loginAsync('user@example.com', 'myPassword');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Build the email to be uploaded.
  const email = new Email();
  email.subject = 'Meeting agenda';
  email.from = 'Alice <alice@example.com>';
  email.addTo('Bob', 'bob@example.com');
  email.body = 'Let\'s meet at 10am to review the agenda.';

  let mimeText: string;
  try {
    mimeText = email.getMime();
  } catch {
    console.log(email.lastErrorText);
    return;
  }

  // Choose the initial flags.  Named variables make each argument's meaning clear at the call.
  const seen = true;
  const flagged = false;
  const answered = false;
  const draft = false;

  // Upload to the Inbox already marked as read (\Seen), with the other flags unset.
  try {
    await imap.appendMimeWithFlagsAsync('Inbox', mimeText, seen, flagged, answered, draft);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  console.log(`Appended UID: ${imap.appendUid}`);

  try {
    await imap.disconnectAsync();
  } catch {
    console.log(imap.lastErrorText);
    return;
  }
}