Unicode C++
Unicode C++
Reading Unread POP3 Email
The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.Chilkat Unicode C++ Downloads
#include <CkMailManW.h>
#include <CkStringBuilderW.h>
#include <CkHashtableW.h>
#include <CkFileAccessW.h>
#include <CkStringTableW.h>
#include <CkEmailW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for receiving (POP3)
// and sending (SMTP) email.
CkMailManW mailman;
// Set the POP3 server's hostname
mailman.put_MailHost(L"pop.example.com");
// Set the POP3 login/password.
mailman.put_PopUsername(L"***");
mailman.put_PopPassword(L"***");
// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
const wchar_t *seenUidlsPath = L"c:/temp/seenUidls.xml";
CkStringBuilderW sbXml;
CkHashtableW htSeenUidls;
CkFileAccessW fac;
if (fac.FileExists(seenUidlsPath) == true) {
success = sbXml.LoadFile(seenUidlsPath,L"utf-8");
if (success == false) {
wprintf(L"%s\n",sbXml.lastErrorText());
return;
}
htSeenUidls.AddFromXmlSb(sbXml);
}
// Get the complete list of UIDLs on the mail server.
CkStringTableW stUidls;
success = mailman.FetchUidls(stUidls);
if (success == false) {
wprintf(L"%s\n",mailman.lastErrorText());
return;
}
// Build a list of unseen UIDLs
CkStringTableW stUnseenUidls;
const wchar_t *uidl = 0;
int i = 0;
int count = stUidls.get_Count();
while (i < count) {
uidl = stUidls.stringAt(i);
if (htSeenUidls.Contains(uidl) != true) {
stUnseenUidls.Append(uidl);
}
i = i + 1;
}
if (stUnseenUidls.get_Count() == 0) {
wprintf(L"No unseen emails!\n");
return;
}
// Download the unseen emails, adding each UIDL to the "seen" hash table.
CkEmailW email;
count = stUnseenUidls.get_Count();
i = 0;
while (i < count) {
// Download the full email.
uidl = stUnseenUidls.stringAt(i);
success = mailman.FetchByUidl(uidl,false,0,email);
if (success == false) {
wprintf(L"%s\n",mailman.lastErrorText());
return;
}
wprintf(L"%d\n",i);
wprintf(L"From: %s\n",email.ck_from());
wprintf(L"Subject: %s\n",email.subject());
// Add this UIDL to the "seen" hash table.
htSeenUidls.AddStr(uidl,L"");
i = i + 1;
}
mailman.Pop3EndSession();
// Update the "seen" UIDLs file.
sbXml.Clear();
htSeenUidls.ToXmlSb(sbXml);
success = sbXml.WriteFile(seenUidlsPath,L"utf-8",false);
if (success == false) {
wprintf(L"%s\n",sbXml.lastErrorText());
return;
}
wprintf(L"Success.\n");
}