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

Delete Individual Emails from POP3 Mailbox

Demonstrates how to delete individual emails from a POP3 mailbox.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // The mailman object is used for receiving (POP3) 
  // and sending (SMTP) email.
  const mailman = new MailMan();

  // Set the POP3 server's hostname
  mailman.mailHost = 'pop.example.com';

  // Set the POP3 login/password.
  mailman.popUsername = 'myLogin';
  mailman.popPassword = 'myPassword';

  // Set the ImmediateDelete property = false
  // When the DeleteEmail method is called, the POP3 session
  // will remain open and the emails will be deleted all at once
  // when the session closes.
  mailman.immediateDelete = false;

  // Download email headers.  
  const bundle = new EmailBundle();
  const keepOnServer = true;
  const headersOnly = true;
  const numBodyLines = 1;
  try {
    await mailman.fetchAllAsync(keepOnServer, headersOnly, numBodyLines, bundle);
  } catch {
    console.log(mailman.lastErrorText);
    return;
  }

  const email = new Email();
  let i = 0;
  while (i < bundle.messageCount) {
    bundle.emailAt(i, email);

    console.log(`From: ${email.from}`);
    console.log(`Subject: ${email.subject}`);

    // If you decide the email is to be deleted, call DeleteEmail.
    // This marks the email for deletion on the POP3 server.
    try {
      await mailman.deleteEmailAsync(email);
    } catch {
      console.log(mailman.lastErrorText);
      return;
    }

    i++;
  }

  // Make sure the POP3 session is ended to finalize the deletes.
  try {
    await mailman.pop3EndSessionAsync();
  } catch {
    console.log(mailman.lastErrorText);
    return;
  }

  console.log('Success!');
}