Sample code for 30+ languages & platforms
C# Requires Chilkat v11.0.0+

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Chilkat.Email email = new Chilkat.Email();
success = email.LoadEml("with-attachments.eml");
if (success != true) {
    Debug.WriteLine(email.LastErrorText);
    return;
}

Chilkat.StringBuilder filename = new Chilkat.StringBuilder();
Chilkat.StringBuilder mediaType = new Chilkat.StringBuilder();
bool found = false;
int i = 0;
while ((i < email.NumAttachments)) {
    filename.SetString(email.GetAttachmentFilename(i));
    mediaType.SetString(email.GetAttachmentContentType(i));
    if ((filename.ContentsEqual("agenda.csv",false) == true) && (mediaType.ContentsEqual("text/csv",false) == true) && (found == false)) {
        Chilkat.BinData data = new Chilkat.BinData();
        success = email.GetAttachmentBd(i,data);
        if (success != true) {
            Debug.WriteLine(email.LastErrorText);
            return;
        }

        success = data.WriteFile("received-agenda.csv");
        if (success != true) {
            Debug.WriteLine("Failed: data.WriteFile.");
            return;
        }

        Debug.WriteLine("Saved attachment: " + Convert.ToString(data.NumBytes) + " bytes.");
        found = true;
    }

    i = i + 1;
}

if (found != true) {
    Debug.WriteLine("No matching attachment.");
}

i = 0;
while ((i < email.NumRelatedItems)) {
    Debug.WriteLine("Related: " + email.GetRelatedContentType(i) + "  Content-ID: " + email.GetRelatedContentID(i));
    i = i + 1;
}