Sample code for 30+ languages & platforms
Xojo Plugin

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

Dim imap As New Chilkat.Imap

// Connect to an IMAP server.
// Use TLS
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example.com")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

// Login
success = imap.Login("myLogin","myPassword")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

// Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

// We can choose to fetch UIDs or sequence numbers.
Dim fetchUids As Boolean
fetchUids = True

// Get the message IDs of all the emails in the mailbox
Dim messageSet As New Chilkat.MessageSet
success = imap.QueryMbx("ALL",fetchUids,messageSet)
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

// Fetch the email headers into a bundle object:
Dim bundle As New Chilkat.EmailBundle
Dim headersOnly As Boolean
headersOnly = True
success = imap.FetchMsgSet(headersOnly,messageSet,bundle)
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

// Scan for emails with attachments, and save the attachments
// to a sub-directory.
Dim fullEmail As New Chilkat.Email
Dim emailHeader As New Chilkat.Email
Dim i As Int32
i = 0
While i < bundle.MessageCount
    // The bundle contains email headers..
    success = bundle.EmailAt(i,emailHeader)

    // Does this email have attachments?
    // Use GetMailNumAttach because the attachments
    // are not actually in the email object because
    // we only downloaded headers.
    Dim numAttach As Int32
    numAttach = imap.GetMailNumAttach(emailHeader)

    If (numAttach > 0) Then
        // 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.
        Dim uidStr As String
        uidStr = emailHeader.GetHeaderField("ckx-imap-uid")
        Dim uid As Int32
        uid = Val(uidStr)

        success = imap.FetchEmail(False,uid,True,fullEmail)
        If (success = False) Then
            System.DebugLog(imap.LastErrorText)
            Return
        End If

        success = fullEmail.SaveAllAttachments("attachmentsDir")

        Dim j As Int32
        For j = 0 To numAttach - 1
            Dim filename As String
            filename = imap.GetMailAttachFilename(emailHeader,j)
            System.DebugLog(filename)
        Next

    End If

    i = i + 1
Wend

// Disconnect from the IMAP server.
success = imap.Disconnect()