C
C
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.Chilkat C Downloads
#include <C_CkImap.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkImap imapSrc;
HCkImap imapDest;
HCkStringBuilder sbMime;
int seqNum;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imapSrc = CkImap_Create();
// Connect to our source IMAP server.
success = CkImap_Connect(imapSrc,"imap.example.com");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapSrc));
CkImap_Dispose(imapSrc);
return;
}
// Login to the source IMAP server
success = CkImap_Login(imapSrc,"admin@chilkatsoft.com","myPassword");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapSrc));
CkImap_Dispose(imapSrc);
return;
}
imapDest = CkImap_Create();
// Connect to our destination IMAP server.
success = CkImap_Connect(imapDest,"mail.example-code.com");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapDest));
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
return;
}
// Login to the destination IMAP server
success = CkImap_Login(imapDest,"myLogin","myPassword");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapDest));
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
return;
}
// Select an IMAP mailbox on the source IMAP server
success = CkImap_SelectMailbox(imapSrc,"Inbox");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapSrc));
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
return;
}
// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
printf("%d\n",CkImap_getNumMessages(imapSrc));
// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.
// This example will copy the first 10 messages using sequence numbers
sbMime = CkStringBuilder_Create();
for (seqNum = 1; seqNum <= 10; seqNum++) {
CkStringBuilder_Clear(sbMime);
success = CkImap_FetchSingleAsMimeSb(imapSrc,seqNum,FALSE,sbMime);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapSrc));
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
CkStringBuilder_Dispose(sbMime);
return;
}
success = CkImap_AppendMimeWithFlagsSb(imapDest,"Inbox",sbMime,FALSE,FALSE,FALSE,FALSE);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imapDest));
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
CkStringBuilder_Dispose(sbMime);
return;
}
}
// Disconnect from the IMAP servers.
success = CkImap_Disconnect(imapSrc);
success = CkImap_Disconnect(imapDest);
CkImap_Dispose(imapSrc);
CkImap_Dispose(imapDest);
CkStringBuilder_Dispose(sbMime);
}