Sample code for 30+ languages & platforms
C++

Remove a Single Attached Message from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessage method, which removes the Nth message/rfc822 sub-part (an attached email) from the message. Indexing begins at 0. This example attaches an email, removes it, and prints the attached-message count before and after.

Background: Attached messages (forwarded emails carried as message/rfc822 parts) are counted and indexed separately from ordinary file attachments and related items. RemoveAttachedMessage targets just one nested email by index — useful, for example, when trimming a bundled digest or stripping a forwarded original before re-sending.

Chilkat C++ Downloads

C++
#include <CkEmail.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the RemoveAttachedMessage method, which removes the Nth message/rfc822
    //  sub-part (attached email) of the email.  The index is zero-based.

    //  Build an inner email and attach it to an outer email.
    CkEmail innerEmail;
    innerEmail.put_Subject("Embedded message");
    innerEmail.put_From("alice@example.com");

    CkEmail email;
    email.put_Subject("Has an attached message");
    success = email.AttachEmail(innerEmail);
    if (success == false) {
        std::cout << email.lastErrorText() << "\r\n";
        return true;
    }

    std::cout << "NumAttachedMessages before = " << email.get_NumAttachedMessages() << "\r\n";

    //  Remove the first attached message (index 0).
    email.RemoveAttachedMessage(0);

    std::cout << "NumAttachedMessages after = " << email.get_NumAttachedMessages() << "\r\n";
    }