Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim email As ChilkatEmail
email.Initialize
success = email.LoadEml("with-attachments.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

Dim filename As ChilkatStringBuilder
filename.Initialize
Dim mediaType As ChilkatStringBuilder
mediaType.Initialize
Dim found As Boolean = False
Dim i As Int = 0
Do While (i < email.NumAttachments)
    filename.SetString(email.GetAttachmentFilename(i))
    mediaType.SetString(email.GetAttachmentContentType(i))
    If (filename.ContentsEqual("agenda.csv", False) = True) And (mediaType.ContentsEqual("text/csv", False) = True) And (found = False) Then
        Dim data As ChilkatBinData
        data.Initialize
        success = email.GetAttachmentBd(i, data)
        If success <> True Then
            Log(email.LastErrorText)
            Return
        End If

        success = data.WriteFile("received-agenda.csv")
        If success <> True Then
            Log("Failed: data.WriteFile.")
            Return
        End If

        Log("Saved attachment: " & data.NumBytes & " bytes.")
        found = True
    End If

    i = i + 1
Loop
If found <> True Then
    Log("No matching attachment.")
End If

i = 0
Do While (i < email.NumRelatedItems)
    Log("Related: " & email.GetRelatedContentType(i) & "  Content-ID: " & email.GetRelatedContentID(i))
    i = i + 1
Loop