Sample code for 30+ languages & platforms
React Native

Append MIME from a StringBuilder with Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a StringBuilder and sets its initial system flags. The first argument is the mailbox, the second is the StringBuilder, and the third through sixth set \Seen, \Flagged, \Answered, and \Draft. This example uploads to Drafts with the draft flag set.

Background: This is the StringBuilder form of AppendMimeWithFlags. Passing the MIME as a StringBuilder avoids copying a large message into an intermediate string and is convenient when you already built or loaded the message into a StringBuilder (for example with LoadFile).

Chilkat React Native Downloads

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

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

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

  // Load the MIME into a StringBuilder.  (A StringBuilder can also be filled via LoadFile.)
  const sbMime = new StringBuilder();
  sbMime.append(mimeText);

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

  // Upload to "Drafts" with the \Draft flag set.
  try {
    await imap.appendMimeWithFlagsSbAsync('Drafts', sbMime, 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;
  }
}