C
C
Download MIME Source of Emails in IMAP Mailbox
Demonstrates how to download the MIME source for emails on an IMAP server.Chilkat C Downloads
#include <C_CkImap.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkImap imap;
int numMessages;
HCkStringBuilder sbMime;
int seqNum;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap = CkImap_Create();
// Connect to an IMAP server.
// Use TLS
CkImap_putSsl(imap,TRUE);
CkImap_putPort(imap,993);
success = CkImap_Connect(imap,"imap.example.com");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// Login
success = CkImap_Login(imap,"myLogin","myPassword");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// Select an IMAP mailbox
success = CkImap_SelectMailbox(imap,"Inbox");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// The NumMessages property contains the number of messages in the selected mailbox.
numMessages = CkImap_getNumMessages(imap);
if (numMessages == 0) {
printf("No messages exist in the Inbox.\n");
CkImap_Dispose(imap);
return;
}
sbMime = CkStringBuilder_Create();
for (seqNum = 1; seqNum <= numMessages; seqNum++) {
CkStringBuilder_Clear(sbMime);
success = CkImap_FetchSingleAsMimeSb(imap,seqNum,FALSE,sbMime);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
CkStringBuilder_Dispose(sbMime);
return;
}
// The MIME source of the downloaded email is contained in sbMime.
}
// Disconnect from the IMAP server.
success = CkImap_Disconnect(imap);
CkImap_Dispose(imap);
CkStringBuilder_Dispose(sbMime);
}