Unicode C
Unicode C
Get IMAP UID from Email Header
See more IMAP Examples
Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.Chilkat Unicode C Downloads
#include <C_CkImapW.h>
#include <C_CkMessageSetW.h>
#include <C_CkEmailBundleW.h>
#include <C_CkEmailW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
BOOL fetchUids;
HCkMessageSetW messageSet;
HCkEmailBundleW bundle;
BOOL headersOnly;
HCkEmailW email;
HCkMessageSetW msgSet1;
int i;
int szBundle;
const wchar_t *uidStr;
success = FALSE;
// This example requires 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"imap.example.com");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Login
success = CkImapW_Login(imap,L"login",L"password");
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;
}
fetchUids = TRUE;
// Get the message UIDs of all the emails in the mailbox
messageSet = CkMessageSetW_Create();
success = CkImapW_QueryMbx(imap,L"ALL",fetchUids,messageSet);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
return;
}
bundle = CkEmailBundleW_Create();
headersOnly = TRUE;
success = CkImapW_FetchMsgSet(imap,headersOnly,messageSet,bundle);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
CkEmailBundleW_Dispose(bundle);
return;
}
// The UID of each fetched email header is available
// in the ckx-imap-uid header field.
email = CkEmailW_Create();
msgSet1 = CkMessageSetW_Create();
i = 0;
szBundle = CkEmailBundleW_getMessageCount(bundle);
while (i < szBundle) {
CkEmailBundleW_EmailAt(bundle,i,email);
// Build a message set containing one UID
uidStr = CkEmailW_getHeaderField(email,L"ckx-imap-uid");
CkMessageSetW_FromCompactString(msgSet1,uidStr);
// Move this message to some other folder.
success = CkImapW_MoveMessages(imap,msgSet1,L"someOtherFolder");
if (success != TRUE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
// Exit the loop...
i = szBundle;
}
i = i + 1;
}
// Disconnect from the IMAP server.
success = CkImapW_Disconnect(imap);
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
CkEmailBundleW_Dispose(bundle);
CkEmailW_Dispose(email);
CkMessageSetW_Dispose(msgSet1);
}