Sample code for 30+ languages & platforms
React Native

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
  // StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
  // number, and the 3rd is the StringBuilder (cleared first).

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

  try {
    await imap.selectMailboxAsync('Inbox');
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  // Search for the message ids to download, returning UIDs.
  const wantUids = true;
  const msgSet = new MessageSet();
  try {
    await imap.queryMbxAsync('ALL', wantUids, msgSet);
  } catch {
    console.log(imap.lastErrorText);
    return;
  }

  if (msgSet.count > 0) {
    const uid = msgSet.getId(0);

    // Download the message's MIME into a StringBuilder.  The id is a UID.
    const useUid = true;
    const sbMime = new StringBuilder();
    try {
      await imap.fetchSingleAsMimeSbAsync(uid, useUid, sbMime);
    } catch {
      console.log(imap.lastErrorText);
      return;
    }

    console.log(`MIME length: ${sbMime.length} chars`);
  }

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