Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oEmail
LOCAL oFilename
LOCAL oMediaType
LOCAL nFound
LOCAL i
LOCAL oData
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:LoadEml("with-attachments.eml")
IF (nSuccess != 1)
? oEmail:LastErrorText
oEmail:destroy()
RETURN
ENDIF
oFilename := CreateObject("Chilkat.StringBuilder")
oMediaType := CreateObject("Chilkat.StringBuilder")
nFound := 0
i := 0
DO WHILE (i < oEmail:NumAttachments)
oFilename:SetString(oEmail:GetAttachmentFilename(i))
oMediaType:SetString(oEmail:GetAttachmentContentType(i))
IF ((oFilename:ContentsEqual("agenda.csv", 0) == 1) .AND. (oMediaType:ContentsEqual("text/csv", 0) == 1) .AND. (nFound == 0))
oData := CreateObject("Chilkat.BinData")
nSuccess := oEmail:GetAttachmentBd(i, oData)
IF (nSuccess != 1)
? oEmail:LastErrorText
oEmail:destroy()
oFilename:destroy()
oMediaType:destroy()
oData:destroy()
RETURN
ENDIF
nSuccess := oData:WriteFile("received-agenda.csv")
IF (nSuccess != 1)
? "Failed: data.WriteFile."
oEmail:destroy()
oFilename:destroy()
oMediaType:destroy()
oData:destroy()
RETURN
ENDIF
? "Saved attachment: " + Str(oData:NumBytes) + " bytes."
nFound := 1
ENDIF
i := i + 1
ENDDO
IF (nFound != 1)
? "No matching attachment."
ENDIF
i := 0
DO WHILE (i < oEmail:NumRelatedItems)
? "Related: " + oEmail:GetRelatedContentType(i) + " Content-ID: " + oEmail:GetRelatedContentID(i)
i := i + 1
ENDDO
oEmail:destroy()
oFilename:destroy()
oMediaType:destroy()
oData:destroy()