Xbase++ Requires Chilkat v11.0.0+
Xbase++
Scan for Emails with Attachments and Save Attachments to Files
Scan for emails with attachments and save attachments.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL oFullEmail
LOCAL oEmailHeader
LOCAL i
LOCAL nNumAttach
LOCAL cUidStr
LOCAL nUid
LOCAL j
LOCAL cFilename
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oImap := CreateObject("Chilkat.Imap")
// Connect to an IMAP server.
// Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Login
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// We can choose to fetch UIDs or sequence numbers.
nFetchUids := 1
// Get the message IDs of all the emails in the mailbox
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
RETURN
ENDIF
// Fetch the email headers into a bundle object:
oBundle := CreateObject("Chilkat.EmailBundle")
nHeadersOnly := 1
nSuccess := oImap:FetchMsgSet(nHeadersOnly, oMessageSet, oBundle)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
RETURN
ENDIF
// Scan for emails with attachments, and save the attachments
// to a sub-directory.
oFullEmail := CreateObject("Chilkat.Email")
oEmailHeader := CreateObject("Chilkat.Email")
i := 0
DO WHILE i < oBundle:MessageCount
// The bundle contains email headers..
oBundle:EmailAt(i, oEmailHeader)
// Does this email have attachments?
// Use GetMailNumAttach because the attachments
// are not actually in the email object because
// we only downloaded headers.
nNumAttach := oImap:GetMailNumAttach(oEmailHeader)
IF (nNumAttach > 0)
// Download the entire email and save the
// attachments. (Remember, we
// need to download the entire email because
// only the headers were previously downloaded.
// The ckx-imap-uid header field is added when
// headers are downloaded. This makes it possible
// to get the UID from the email object.
cUidStr := oEmailHeader:GetHeaderField("ckx-imap-uid")
nUid := Val(cUidStr)
nSuccess := oImap:FetchEmail(0, nUid, 1, oFullEmail)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oFullEmail:destroy()
oEmailHeader:destroy()
RETURN
ENDIF
nSuccess := oFullEmail:SaveAllAttachments("attachmentsDir")
FOR j := 0 TO nNumAttach - 1
cFilename := oImap:GetMailAttachFilename(oEmailHeader, j)
? cFilename
NEXT
ENDIF
i := i + 1
ENDDO
// Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oFullEmail:destroy()
oEmailHeader:destroy()