![]() |
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
(Android™) 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.
// Important: Don't forget to include the call to System.loadLibrary // as shown at the bottom of this code sample. package com.test; import android.app.Activity; import com.chilkatsoft.*; import android.widget.TextView; import android.os.Bundle; public class SimpleActivity extends Activity { private static final String TAG = "Chilkat"; // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean 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. CkMailMan mailman = new CkMailMan(); // Set the POP3 server's hostname mailman.put_MailHost("pop.example.com"); // Set the POP3 login/password. mailman.put_PopUsername("***"); mailman.put_PopPassword("***"); // Keep a records of already-seen UIDLs in hash table serialized to an XML file. String seenUidlsPath = "c:/temp/seenUidls.xml"; CkStringBuilder sbXml = new CkStringBuilder(); CkHashtable htSeenUidls = new CkHashtable(); CkFileAccess fac = new CkFileAccess(); if (fac.FileExists(seenUidlsPath) == true) { success = sbXml.LoadFile(seenUidlsPath,"utf-8"); if (success == false) { Log.i(TAG, sbXml.lastErrorText()); return; } htSeenUidls.AddFromXmlSb(sbXml); } // Get the complete list of UIDLs on the mail server. CkStringTable stUidls = new CkStringTable(); success = mailman.FetchUidls(stUidls); if (success == false) { Log.i(TAG, mailman.lastErrorText()); return; } // Build a list of unseen UIDLs CkStringTable stUnseenUidls = new CkStringTable(); String uidl; 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) { Log.i(TAG, "No unseen emails!"); return; } // Download the unseen emails, adding each UIDL to the "seen" hash table. CkEmail email = new CkEmail(); 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) { Log.i(TAG, mailman.lastErrorText()); return; } Log.i(TAG, String.valueOf(i)); Log.i(TAG, "From: " + email.ck_from()); Log.i(TAG, "Subject: " + email.subject()); // Add this UIDL to the "seen" hash table. htSeenUidls.AddStr(uidl,""); i = i + 1; } mailman.Pop3EndSession(); // Update the "seen" UIDLs file. sbXml.Clear(); htSeenUidls.ToXmlSb(sbXml); success = sbXml.WriteFile(seenUidlsPath,"utf-8",false); if (success == false) { Log.i(TAG, sbXml.lastErrorText()); return; } Log.i(TAG, "Success."); } static { System.loadLibrary("chilkat"); // Note: If the incorrect library name is passed to System.loadLibrary, // then you will see the following error message at application startup: //"The application <your-application-name> has stopped unexpectedly. Please try again." } } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.