Unicode C
Unicode C
Fetch Oldest/Newest IMAP Email
Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.Chilkat Unicode C Downloads
#include <C_CkImapW.h>
#include <C_CkEmailW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
int n;
BOOL isUid;
HCkEmailW oldestEmail;
HCkEmailW newestEmail;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap = CkImapW_Create();
// Connect to an IMAP server.
// Use TLS
CkImapW_putSsl(imap,TRUE);
CkImapW_putPort(imap,993);
success = CkImapW_Connect(imap,L"mail.testemail.net");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Login
success = CkImapW_Login(imap,L"***",L"***");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Select an IMAP mailbox
success = CkImapW_SelectMailbox(imap,L"Inbox");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
n = CkImapW_getNumMessages(imap);
if (n > 0) {
// The oldest email is always at sequence number 1.
isUid = FALSE;
oldestEmail = CkEmailW_Create();
success = CkImapW_FetchEmail(imap,FALSE,1,isUid,oldestEmail);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkEmailW_Dispose(oldestEmail);
return;
}
// Display the From and Subject
wprintf(L"%s\n",CkEmailW_fromAddress(oldestEmail));
wprintf(L"%s\n",CkEmailW_subject(oldestEmail));
wprintf(L"--\n");
// The newest email is at sequence number N:
newestEmail = CkEmailW_Create();
success = CkImapW_FetchEmail(imap,FALSE,n,isUid,newestEmail);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkEmailW_Dispose(oldestEmail);
CkEmailW_Dispose(newestEmail);
return;
}
// Display the From and Subject
wprintf(L"%s\n",CkEmailW_fromAddress(newestEmail));
wprintf(L"%s\n",CkEmailW_subject(newestEmail));
}
// Disconnect from the IMAP server.
success = CkImapW_Disconnect(imap);
CkImapW_Dispose(imap);
CkEmailW_Dispose(oldestEmail);
CkEmailW_Dispose(newestEmail);
}