Provides properties and methods for presenting UIDL item.
IDispatch
EAGetMail.UIDLItem
[Visual Basic] Public Class UIDLItem
[C++] public: interface IUIDLItem
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Public Properties
| DownloadTime | Gets date time that UIDLItem instance was added. |
| FileName | The file name associated with UIDLItem instance. |
| Flags | The integer value associated with UIDLItem instance. |
| ServerUser | Gets server+"#"+user value of the UIDLItem. |
| UIDL | Gets UIDL value. |
Example
[[Visual Basic 6.0, VBScript, Visual C++, Delphi] The following example demonstrates how To retrieve email with EAGetMail POP3 & IMAP Component, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.
[Visual Basic 6.0]
Const MailServerPop3 = 0
Const MailServerImap4 = 1
Const MailServerEWS = 2
Const MailServerDAV = 3
Const MailServerMsGraph = 4
Public Sub ReceiveMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean, _
ByVal bLeaveCopy)
Dim currentPath As String
Dim localInbox As String
Dim isUidlLoaded As Boolean
isUidlLoaded = False
' Create a folder named "inbox" under current directory
' to save the email retrieved.
currentPath = App.Path
localInbox = currentPath & "\inbox"
Dim oTools As New EAGetMailObjLib.Tools
oTools.CreateFolder localInbox
Dim oServer As New EAGetMailObjLib.MailServer
oServer.Server = sServer
oServer.User = sUserName
oServer.Password = sPassword
oServer.Protocol = MailServerPop3
oServer.SSLConnection = bSSLConnection
If oServer.SSLConnection Then
oServer.Port = 995
Else
oServer.Port = 110
End If
On Error GoTo ErrorHandle:
' uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
' the same email from server more than once, we record the email uidl retrieved every time
' if you delete the email from server every time and not to leave a copy of email on
' the server, then please remove all the function about uidl.
' If you want to re-download all emails, please delete uidl.txt from local inbox.
' uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
' the same email from server more than once, we record the email uidl retrieved every time
' if you delete the email from server every time and not to leave a copy of email on
' the server, then please remove all the function about uidl.
' If you want to re-download all emails, please delete uidl.txt from local inbox.
Dim oUIDLManager As New EAGetMailObjLib.UIDLManager
oUIDLManager.Load localInbox & "\uidl.txt"
isUidlLoaded = True
Dim oClient As New EAGetMailObjLib.MailClient
oClient.LicenseCode = "TryIt"
oClient.Connect oServer
MsgBox "Connected"
Dim mailInfos As EAGetMailObjLib.MailInfoCollection
Set mailInfos = oClient.GetMailInfoList()
MsgBox mailInfos.Count & " emails"
' Remove the local uidl that is not existed on the server,
oUIDLManager.SyncUIDLEX oServer, mailInfos
' Update result back to uidl file
oUIDLManager.Update
Dim i As Long
For i = 0 To mailInfos.Count - 1
Dim oInfo As EAGetMailObjLib.MailInfo
Set oInfo = mailInfos.Item(i)
MsgBox "Index: " & i + 1 & "; Size: " & oInfo.Size & _
"; UIDL: " & oInfo.UIDL
Dim oUIDLItem As UIDLItem
Set oUIDLItem = oUIDLManager.FindUIDL(oServer, oInfo.UIDL)
' If this email has not been retrieved before, then get it
If oUIDLItem Is Nothing Then
Dim fileName As String
fileName = oTools.GenFileName(i + 1) & ".eml"
Dim fullFileName As String
fullFileName = localInbox & "\" & fileName
Dim oMail As EAGetMailObjLib.Mail
Set oMail = oClient.GetMail(oInfo)
oMail.SaveAs fullFileName, True
If bLeaveCopy Then
' Add the email uidl to uidl file to avoid we retrieve it next time.
oUIDLManager.AddUIDL oServer, oInfo.UIDL, fileName
Else
oClient.Delete oInfo
' Remove UIDL from local uidl file.
oUIDLManager.RemoveUIDL oServer, oInfo.UIDL
End If
End If
Next
' Quit
oClient.Quit
' Update the uidl list to a text file and then we can load it next time.
oUIDLManager.Update
Exit Sub
ErrorHandle:
MsgBox Err.Description
If isUidlLoaded Then
' Update the uidl list to a text file and then we can load it next time.
oUIDLManager.Update
End If
End Sub
[VBScript]
Const MailServerPop3 = 0
Const MailServerImap4 = 1
Const MailServerEWS = 2
Const MailServerDAV = 3
Const MailServerMsGraph = 4
Public Sub ReceiveMail( _
ByVal sServer , _
ByVal sUserName, _
ByVal sPassword, _
ByVal bSSLConnection, _
ByVal bLeaveCopy)
Dim localInbox
Dim isUidlLoaded
isUidlLoaded = False
' Create a folder named "inbox"
' to save the email retrieved.
localInbox = "d:\inbox"
Dim oTools
Set oTools = CreateObject("EAGetMailObj.Tools")
oTools.CreateFolder localInbox
Dim oServer
Set oServer = CreateObject("EAGetMailObj.MailServer")
oServer.Server = sServer
oServer.User = sUserName
oServer.Password = sPassword
oServer.Protocol = MailServerPop3
oServer.SSLConnection = bSSLConnection
If oServer.SSLConnection Then
oServer.Port = 995
Else
oServer.Port = 110
End If
' uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
' the same email from server more than once, we record the email uidl retrieved every time
' if you delete the email from server every time and not to leave a copy of email on
' the server, then please remove all the function about uidl.
' If you want to re-download all emails, please delete uidl.txt from local inbox.
' uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
' the same email from server more than once, we record the email uidl retrieved every time
' if you delete the email from server every time and not to leave a copy of email on
' the server, then please remove all the function about uidl.
' If you want to re-download all emails, please delete uidl.txt from local inbox.
Dim oUIDLManager
Set oUIDLManager = CreateObject("EAGetMailObj.UIDLManager")
oUIDLManager.Load localInbox & "\uidl.txt"
isUidlLoaded = True
Dim oClient
Set oClient = CreateObject("EAGetMailObj.MailClient")
oClient.LicenseCode = "TryIt"
oClient.Connect oServer
WScript.Echo "Connected"
Dim mailInfos
Set mailInfos = oClient.GetMailInfoList()
WScript.Echo mailInfos.Count & " emails"
' Remove the local uidl that is not existed on the server,
oUIDLManager.SyncUIDLEX oServer, mailInfos
' Update result back to uidl file
oUIDLManager.Update
Dim i
For i = 0 To mailInfos.Count - 1
Dim oInfo
Set oInfo = mailInfos.Item(i)
WScript.Echo "Index: " & i + 1 & "; Size: " & oInfo.Size & _
"; UIDL: " & oInfo.UIDL
Dim oUIDLItem
Set oUIDLItem = oUIDLManager.FindUIDL(oServer, oInfo.UIDL)
' If this email has not been retrieved before, then get it
If oUIDLItem Is Nothing Then
Dim fileName
fileName = oTools.GenFileName(i + 1) & ".eml"
Dim fullFileName
fullFileName = localInbox & "\" & fileName
Dim oMail
Set oMail = oClient.GetMail(oInfo)
oMail.SaveAs fullFileName, True
If bLeaveCopy Then
' Add the email uidl to uidl file to avoid we retrieve it next time.
oUIDLManager.AddUIDL oServer, oInfo.UIDL, fileName
Else
oClient.Delete oInfo
' Remove UIDL from local uidl file.
oUIDLManager.RemoveUIDL oServer, oInfo.UIDL
End If
End If
Next
' Quit
oClient.Quit
' Update the uidl list to a text file and then we can load it next time.
oUIDLManager.Update
End Sub
[Visual C++]
#include "stdafx.h"
#include <windows.h>
#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;
DWORD _getCurrentPath(LPTSTR lpPath, DWORD nSize)
{
DWORD dwSize = ::GetModuleFileName(NULL, lpPath, nSize);
if (dwSize == 0 || dwSize == nSize)
{
return 0;
}
// Change file name to current full path
LPCTSTR psz = _tcsrchr(lpPath, _T('\\'));
if (psz != NULL)
{
lpPath[psz - lpPath] = _T('\0');
return _tcslen(lpPath);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int MailServerPop3 = 0;
const int MailServerImap4 = 1;
const int MailServerEWS = 2;
const int MailServerDAV = 3;
const int MailServerMsGraph = 4;
// Initialize COM environment
::CoInitialize(NULL);
/// Create a folder named "inbox" under current exe file directory
// to save the emails retrieved.
TCHAR szPath[MAX_PATH + 1];
_getCurrentPath(szPath, MAX_PATH);
TCHAR szMailBox[MAX_PATH + 1];
wsprintf(szMailBox, _T("%s\\inbox"), szPath);
// Create a folder to store emails
::CreateDirectory(szMailBox, NULL);
IUIDLManagerPtr uidlManager;
bool isUidlLoaded = false;
// leave a copy of message on server
bool leaveCopy = true;
try
{
IMailServerPtr oServer = NULL;
oServer.CreateInstance(__uuidof(EAGetMailObjLib::MailServer));
oServer->Server = _T("pop3.emailarchitect.net");
oServer->User = _T("test@emailarchitect.net");
oServer->Password = _T("testpassword");
oServer->Protocol = MailServerPop3;
// Enable SSL/TLS connection, most modern email servers require SSL/TLS by default
oServer->SSLConnection = VARIANT_TRUE;
oServer->Port = 995;
// If your POP3 doesn't deploy SSL connection
// Please use
// oServer->SSLConnection = VARIANT_FALSE;
// oServer->Port = 110;
IMailClientPtr oClient = NULL;
oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient));
oClient->LicenseCode = _T("TryIt");
// uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
// the same email from server more than once, we record the email uidl retrieved every time
// if you delete the email from server every time and not to leave a copy of email on
// the server, then please remove all the function about uidl.
TCHAR szUidlFile[MAX_PATH + 1];
wsprintf(szUidlFile, _T("%s\\uidl.txt"), szMailBox);
uidlManager.CreateInstance(__uuidof(EAGetMailObjLib::UIDLManager));
uidlManager->Load(szUidlFile);
isUidlLoaded = true;
oClient->Connect(oServer);
_tprintf(_T("Connected\r\n"));
IMailInfoCollectionPtr infos = oClient->GetMailInfoList();
_tprintf(_T("Total %d emails\r\n"), infos->Count);
// Remove the local uidl that is not existed on the server,
uidlManager->SyncUIDLEX(oServer, infos);
uidlManager->Update();
for (long i = 0; i < infos->Count; i++)
{
IMailInfoPtr pInfo = infos->GetItem(i);
IUIDLItemPtr uidl = uidlManager->FindUIDL(oServer, pInfo->UIDL);
if (uidl != nullptr)
{
// This email has been downloaded before
continue;
}
// Generate an unique file named based on current date time
// If you don't like it, you can use your method to generate file name
TCHAR szFile[MAX_PATH + 1];
SYSTEMTIME curtm;
::GetLocalTime(&curtm);
wsprintf(szFile, _T("%04d%02d%02d%02d%02d%02d%03d%d.eml"),
curtm.wYear,
curtm.wMonth,
curtm.wDay,
curtm.wHour,
curtm.wMinute,
curtm.wSecond,
curtm.wMilliseconds,
i);
TCHAR szFullPath[MAX_PATH + 1];
wsprintf(szFullPath, _T("%s\\%s"),
szMailBox, szFile);
IMailPtr oMail = oClient->GetMail(pInfo);
oMail->SaveAs(szFullPath, VARIANT_TRUE);
if(leaveCopy)
{
// Add the email uidl to uidl file to avoid we retrieve it next time.
uidlManager->AddUIDL(oServer, pInfo->UIDL, szFile);
}
else
{
oClient->Delete(pInfo);
// Remove UIDL from local uidl file.
uidlManager->RemoveUIDL(oServer, pInfo->UIDL);
}
}
}
catch(_com_error &ep)
{
_tprintf(_T("Error: %s"), (const TCHAR*)ep.Description());
}
//update the uidl list to a text file and then we can load it next time.
if (uidlManager != nullptr && isUidlLoaded)
{
uidlManager->Update();
}
return 0;
}
[Delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils, EAGetMailObjLib_TLB; // Add EAGetMail unit
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
MailServerPop3 = 0;
MailServerImap4 = 1;
MailServerEWS = 2;
MailServerDAV = 3;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
oServer: TMailServer;
oClient: TMailClient;
oTools: TTools;
oMail: IMail;
oUIDLManager: TUIDLManager;
infos: IMailInfoCollection;
oInfo: IMailInfo;
localInbox, fileName, fullFileName: WideString;
isUidlLoaded: Boolean;
leaveCopy: Boolean;
i: integer;
begin
// set current thread code page to system default code page.
SetThreadLocale(GetSystemDefaultLCID());
// leave a copy of message on server
leaveCopy := true;
isUidlLoaded := false;
// uidl is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
// the same email from server more than once, we record the email uidl retrieved every time
// if you delete the email from server every time and not to leave a copy of email on
// the server, then please remove all the function about uidl.
oUIDLManager := TUIDLManager.Create(Application);
try
oTools := TTools.Create(Application);
localInbox := GetCurrentDir() + '\inbox';
oTools.CreateFolder(localInbox);
oUIDLManager.Load(localInbox + '\uidl.txt');
isUidlLoaded := true;
oServer := TMailServer.Create(Application);
oServer.Server := 'pop3.emailarchitect.net';
oServer.User := 'test@emailarchitect.net';
oServer.Password := 'testpassword';
oServer.Protocol := MailServerPop3;
// Enable SSL/TLS Connection, most modern email server require SSL/TLS connection by default.
oServer.SSLConnection := true;
// Set 995 SSL POP3 port
oServer.Port := 995;
// If your POP3 server doesn't deploy SSL connection
// Please use
// oServer.SSLConnection := false;
// oServer.Port := 110;
oClient := TMailClient.Create(Application);
oClient.LicenseCode := 'TryIt';
oClient.Connect1(oServer.DefaultInterface);
ShowMessage('Connected!');
infos := oClient.GetMailInfoList();
ShowMessage(Format('Total %d email(s)', [infos.Count]));
// Remove the local uidl that is not existed on the server,
oUIDLManager.SyncUIDLEX(oServer.DefaultInterface, infos);
oUIDLManager.Update();
for i := 0 to infos.Count - 1 do
begin
oInfo := infos.Item[i];
// This email has not been retrieved before, then get it
if oUIDLManager.FindUIDL(oServer.DefaultInterface, oInfo.UIDL) <> nil then
continue; // this email has been downloaded, do not receive it again
// Generate a random file name by current local datetime,
// You can use your method to generate the filename if you do not like it
fileName := oTools.GenFileName(i) + '.eml';
fullFileName := localInbox + '\' + fileName;
// Receive email from email server
oMail := oClient.GetMail(oInfo);
ShowMessage('From: ' + oMail.From.Address + #13#10 +
'Subject: ' + oMail.Subject);
// Save email to local disk
oMail.SaveAs(fullFileName, true);
if leaveCopy Then
begin
// Add the email uidl to uidl file to avoid we retrieve it next time.
oUIDLManager.AddUIDL(oServer.DefaultInterface, oInfo.UIDL, fileName);
end
else
begin
oClient.Delete(oInfo);
// Remove UIDL from local uidl file.
oUIDLManager.RemoveUIDL(oServer.DefaultInterface, oInfo.UIDL);
end;
end;
// Quit and expunge emails marked as deleted from email server
oClient.Quit;
except
on ep:Exception do
ShowMessage('Error: ' + ep.Message);
end;
// Update the uidl list to a text file and then we can load it next time.
if isUidlLoaded then
oUIDLManager.Update();
end;
end.
See Also
Online Tutorials
Using UIDL Function to Mark the Email has been downloaded/read in VB6 - Tutorial
Using UIDL Function to Mark the Email has been downloaded/read in Delphi - Tutorial
Using UIDL Function to Mark the Email has been downloaded/read VC++ - Tutorial