MailClient.Expunge Method


expunges the deleted emails in current selected folder on IMAP4 server.

[Visual Basic 6.0]
Public Sub Expunge( _
)
[Visual C++]
public: HRESULT Expunge(
);

Remarks

MailClient.Delete method only marks the email as deleted, only the MailClient.Quit method (POP3 and IMAP4) or MailClient.Expunge method(IMAP4 only) expunges the deleted email from server. For POP3, deleted flag will lose if the connection is closed; FOR IMAP4, the deleted flag is permant even the connection is closed.

Example

[Visual Basic 6.0, VBScript, Visual C++, Delphi] The following example demonstrates how to delete and expunge email with EAGetMail POP3 & IMAP ActiveX Object. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Public Sub ExpungeMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean)
    
    Const MailServerPop3 = 0
    Const MailServerImap4 = 1
    Const MailServerEWS = 2
    Const MailServerDAV = 3
    Const MailServerMsGraph = 4

    'For evaluation usage, please use "TryIt" as the license code, otherwise the
    '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
    '"trial version expired" exception will be thrown.
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"
   
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = sServer
    oServer.User = sUserName
    oServer.Password = sPassword
    oServer.SSLConnection = bSSLConnection
    oServer.Protocol = MailServerImap4
    
    If oServer.SSLConnection Then
        oServer.Port = 993 'SSL IMAP4
    Else
        oServer.Port = 143 'IMAP4 normal
    End If
    
On Error GoTo ErrorHandle
    oClient.Connect oServer

    Dim infos As EAGetMailObjLib.MailInfoCollection
    Set infos = oClient.GetMailInfoList()
    
    Dim i
    For i = 0 To infos.Count - 1
        Dim info As EAGetMailObjLib.MailInfo
        Set info = infos.Item(i)
        
        ' Delete email from server
        oClient.Delete info
    Next
    
    ' Delete method just mark the email as deleted,
    ' Expunge method purge the emails from server exactly.
    oClient.Expunge
    oClient.Logout

    Exit Sub
    
ErrorHandle:
    ''Error handle
    MsgBox Err.Description
    
    oClient.Close
End Sub


[VBScript] Sub ExpungeMail( _ ByVal sServer, _ ByVal sUserName, _ ByVal sPassword, _ ByVal bSSLConnection) Const MailServerPop3 = 0 Const MailServerImap4 = 1 Const MailServerEWS = 2 Const MailServerDAV = 3 Const MailServerMsGraph = 4 'For evaluation usage, please use "TryIt" as the license code, otherwise the '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then '"trial version expired" exception will be thrown. Dim oClient Set oClient = CreateObject("EAGetMailObj.MailClient") oClient.LicenseCode = "TryIt" Dim oServer Set oServer = CreateObject("EAGetMailObj.MailServer") oServer.Server = sServer oServer.User = sUserName oServer.Password = sPassword oServer.SSLConnection = bSSLConnection oServer.Protocol = MailServerImap4 If oServer.SSLConnection Then oServer.Port = 993 'SSL IMAP4 Else oServer.Port = 143 'IMAP4 normal End If oClient.Connect oServer Dim infos Set infos = oClient.GetMailInfoList() Dim i For i = 0 To infos.Count - 1 Dim info Set info = infos.Item(i) ' Delete email from server oClient.Delete info Next ' Delete method just mark the email as deleted, ' Expunge method purge the emails from server exactly. oClient.Expunge oClient.Logout End Sub
[Visual C++] #include "stdafx.h" #include <windows.h> #include "eagetmailobj.tlh" using namespace EAGetMailObjLib; void ExpungeMail( LPCTSTR sServer, LPCTSTR sUserName, LPCTSTR sPassword, bool bSSLConnection) { ::CoInitialize(NULL); const int MailServerPop3 = 0; const int MailServerImap4 = 1; const int MailServerEWS = 2; const int MailServerDAV = 3; const int MailServerMsGraph = 4; try { IMailClientPtr oClient; oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient)); IMailServerPtr oServer; oServer.CreateInstance(__uuidof(EAGetMailObjLib::MailServer)); // For evaluation usage, please use "TryIt" as the license code, otherwise the // "invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then // "trial version expired" exception will be thrown. oClient->LicenseCode = _T("TryIt"); oServer->Server = sServer; oServer->User = sUserName; oServer->Password = sPassword; oServer->Protocol = MailServerImap4; if(bSSLConnection) { oServer->Port = 993; oServer->SSLConnection = VARIANT_TRUE; } else { oServer->Port = 143; } oClient->Connect(oServer); IMailInfoCollectionPtr infos = oClient->GetMailInfoList(); for(long i = 0; i < infos->Count; i++) { IMailInfoPtr pInfo = infos->GetItem(i); // delete email from server oClient->Delete(pInfo); } // Delete method just mark the email as deleted, // Expunge method purge the emails from server exactly. oClient->Expunge(); oClient->Logout(); } catch(_com_error &ep) { _tprintf(_T("ERROR: %s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }
[Delphi] const MailServerPop3 = 0; MailServerImap4 = 1; MailServerEWS = 2; MailServerDAV = 3; procedure ExpungeMail(server: WideString; user: WideString; password: WideString; useSslConnection: Boolean); var oServer: TMailServer; oClient: TMailClient; folders: IFolderCollection; oFolder, destFolder: IImap4Folder; infos: IMailInfoCollection; oInfo: IMailInfo; i: Integer; begin try oServer := TMailServer.Create(Application); oServer.Server := server; oServer.User := user; oServer.Password := password; oServer.Protocol := MailServerImap4; // Enable SSL/TLS Connection, most modern email server require SSL/TLS connection by default. oServer.SSLConnection := useSslConnection; if useSslConnection then begin // Set 993 SSL IMAP port oServer.Port := 993; end else begin // Set 143 IMAP port oServer.Port := 143; end; oClient := TMailClient.Create(Application); oClient.LicenseCode := 'TryIt'; oClient.Connect1(oServer.DefaultInterface); ShowMessage('Connected!'); infos := oClient.GetMailInfoList(); for i := 0 to infos.Count - 1 do begin oInfo := infos.Item[i]; // delete email from server oClient.Delete(oInfo); end; // Delete method just mark the email as deleted, // Expunge method purge the emails from server exactly. oClient.Expunge(); oClient.Logout(); except on ep:Exception do ShowMessage('Error: ' + ep.Message); end; end;

See Also

MailClient.Delete Method
MailClient.Reset Method