Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkImap.h>
#include <CkMessageSet.h>
#include <CkEmail.h>
#include <CkEmailBundle.h>

void ChilkatSample(void)
    {
    CkString strOut;

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

    CkImap imap;

    // Connect to an IMAP server.
    // Use TLS
    imap.put_Ssl(true);
    imap.put_Port(993);
    bool success = imap.Connect("imap.someMailServer.com");
    if (success != true) {
        strOut.append(imap.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Login
    success = imap.Login("****","****");
    if (success != true) {
        strOut.append(imap.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Select an IMAP mailbox
    success = imap.SelectMailbox("Inbox");
    if (success != true) {
        strOut.append(imap.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    CkMessageSet *messageSet = 0;
    // We can choose to fetch UIDs or sequence numbers.
    bool fetchUids = true;
    // Get the message IDs of all the emails in the mailbox
    messageSet = imap.Search("ALL",fetchUids);
    if (imap.get_LastMethodSuccess() == false) {
        strOut.append(imap.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    int numFound = messageSet->get_Count();
    if (numFound == 0) {
        strOut.append("No messages found.");
        strOut.append("\r\n");
        delete messageSet;
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Get the 1st 10 messages in messageSet.
    int upperBound = 10;
    if (numFound < upperBound) {
        upperBound = numFound;
    }

    int i = 0;
    bool bUid = messageSet->get_HasUids();
    while (i < upperBound) {

        CkEmail *email = imap.FetchSingleHeader(messageSet->GetId(i),bUid);
        if (imap.get_LastMethodSuccess() == false) {
            strOut.append(imap.lastErrorText());
            strOut.append("\r\n");
            delete messageSet;
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;

        }
        else {
            // Do whatever is needed with the email.. 
            // ....

            delete email;
        }

        i = i + 1;
    }

    // Alternatively, create a new message set containing the 1st 10 message id's (UID's) 
    // from the search results message set:
    CkMessageSet firstTen;

    for (i = 0; i <= upperBound - 1; i++) {
        firstTen.InsertId(messageSet->GetId(i));
    }

    delete messageSet;

    // Download the 1st ten headers:
    CkEmailBundle *bundle = 0;
    bundle = imap.FetchHeaders(firstTen);
    if (imap.get_LastMethodSuccess() == false) {
        delete firstTen;
        strOut.append(imap.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Other examples show how to iterate over the emails contained within the bundle...

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

    delete firstTen;
    delete bundle;


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.