![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Reading Unread POP3 EmailThe 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. Note: This example requires Chilkat v11.0.0 or greater.
#include <C_CkMailManW.h> #include <C_CkStringBuilderW.h> #include <C_CkHashtableW.h> #include <C_CkFileAccessW.h> #include <C_CkStringTableW.h> #include <C_CkEmailW.h> void ChilkatSample(void) { BOOL success; HCkMailManW mailman; const wchar_t *seenUidlsPath; HCkStringBuilderW sbXml; HCkHashtableW htSeenUidls; HCkFileAccessW fac; HCkStringTableW stUidls; HCkStringTableW stUnseenUidls; const wchar_t *uidl; int i; int count; HCkEmailW email; 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. mailman = CkMailManW_Create(); // Set the POP3 server's hostname CkMailManW_putMailHost(mailman,L"pop.example.com"); // Set the POP3 login/password. CkMailManW_putPopUsername(mailman,L"***"); CkMailManW_putPopPassword(mailman,L"***"); // Keep a records of already-seen UIDLs in hash table serialized to an XML file. seenUidlsPath = L"c:/temp/seenUidls.xml"; sbXml = CkStringBuilderW_Create(); htSeenUidls = CkHashtableW_Create(); fac = CkFileAccessW_Create(); if (CkFileAccessW_FileExists(fac,seenUidlsPath) == TRUE) { success = CkStringBuilderW_LoadFile(sbXml,seenUidlsPath,L"utf-8"); if (success == FALSE) { wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbXml)); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); return; } CkHashtableW_AddFromXmlSb(htSeenUidls,sbXml); } // Get the complete list of UIDLs on the mail server. stUidls = CkStringTableW_Create(); success = CkMailManW_FetchUidls(mailman,stUidls); if (success == FALSE) { wprintf(L"%s\n",CkMailManW_lastErrorText(mailman)); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); CkStringTableW_Dispose(stUidls); return; } // Build a list of unseen UIDLs stUnseenUidls = CkStringTableW_Create(); i = 0; count = CkStringTableW_getCount(stUidls); while (i < count) { uidl = CkStringTableW_stringAt(stUidls,i); if (CkHashtableW_Contains(htSeenUidls,uidl) != TRUE) { CkStringTableW_Append(stUnseenUidls,uidl); } i = i + 1; } if (CkStringTableW_getCount(stUnseenUidls) == 0) { wprintf(L"No unseen emails!\n"); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); CkStringTableW_Dispose(stUidls); CkStringTableW_Dispose(stUnseenUidls); return; } // Download the unseen emails, adding each UIDL to the "seen" hash table. email = CkEmailW_Create(); count = CkStringTableW_getCount(stUnseenUidls); i = 0; while (i < count) { // Download the full email. uidl = CkStringTableW_stringAt(stUnseenUidls,i); success = CkMailManW_FetchByUidl(mailman,uidl,FALSE,0,email); if (success == FALSE) { wprintf(L"%s\n",CkMailManW_lastErrorText(mailman)); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); CkStringTableW_Dispose(stUidls); CkStringTableW_Dispose(stUnseenUidls); CkEmailW_Dispose(email); return; } wprintf(L"%d\n",i); wprintf(L"From: %s\n",CkEmailW_ck_from(email)); wprintf(L"Subject: %s\n",CkEmailW_subject(email)); // Add this UIDL to the "seen" hash table. CkHashtableW_AddStr(htSeenUidls,uidl,L""); i = i + 1; } CkMailManW_Pop3EndSession(mailman); // Update the "seen" UIDLs file. CkStringBuilderW_Clear(sbXml); CkHashtableW_ToXmlSb(htSeenUidls,sbXml); success = CkStringBuilderW_WriteFile(sbXml,seenUidlsPath,L"utf-8",FALSE); if (success == FALSE) { wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbXml)); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); CkStringTableW_Dispose(stUidls); CkStringTableW_Dispose(stUnseenUidls); CkEmailW_Dispose(email); return; } wprintf(L"Success.\n"); CkMailManW_Dispose(mailman); CkStringBuilderW_Dispose(sbXml); CkHashtableW_Dispose(htSeenUidls); CkFileAccessW_Dispose(fac); CkStringTableW_Dispose(stUidls); CkStringTableW_Dispose(stUnseenUidls); CkEmailW_Dispose(email); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.