Leave a copy of message on server and how to detect if an email is new


Many developers asked us this question, so this article will introduce how to implement it with ANPOP POP3 Component.

Leave a copy of message on server

To delete email from POP3 serve, you must invoke Delete method of POPMAIN object explicitly, otherwise this email would be left on the server, and you can retrieve it next time. But the problem is, you don't want to receive it next time, to solve it, please refer to the following section.

How to detect if the email is new

Basically, pop3 protocol doesn't support this function. However, you can implement this with message-id. Message-Id is an unique identifier of email on POP3 server. Your application can get message-id of a specified email by GetMsgID method of POPMAIN object.

Firstly, your application should record message-id of email retrieved to a local message-id list. Next time before you retrieve email, compare local message-id with remote message-id. If this message-id exists in your local message-id list, then it is old, otherwise it is new.

The following sample demonstrates how to implement it in VBScript.

Option Explicit

Dim messageIds, newmessageIds

Sub LoadMessageIds
   Const ForReading = 1
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile( "c:\messageIds.txt", ForReading, True )
   On Error Resume Next
   messageIds = f.ReadAll
   f.Close()
End Sub

Function IsMessageIdExisted( messageId )
    Dim pos
    pos = InStr( 1, messageIds, "<" & messageId & ">" )
    If pos > 0 Then
        IsMessageIdExisted = True
    Else
        IsMessageIdExisted = False
    End If
End Function

Sub SaveMessageIds
   Const ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:\messageIds.txt", ForWriting, True)
   f.Write newmessageIds
   f.Close()
End Sub

Sub Retrieve( server, user, password )
    Dim oPop3, oMsg, nRet, i, nCount, messageId, emailContent
    WScript.Echo "Connecting " & args(0) & " ..."
    newmessageIds = ""

    Set oPop3 = CreateObject("ANPOP.POPMAIN")
    Set oMsg = CreateObject("ANPOP.POPMSG")

    nRet = oPop3.Connect(server, user, password )
    If nRet <> 0 Then
        WScript.Echo "Error with connect server, please make sure server, user, password are correct"
        WScript.Quit
    End If

    nCount = oPop3.GetTotalOfMails()
    If nCount = -1 Then
        WScript.Echo "Error with GetTotalOfMails method"
        WScript.Quit
    End If

    WScript.Echo "Total " & nCount & " email(s)" & Chr(13) & Chr(10)

    For i = 1 To nCount
        messageId = oPop3.GetMsgID(i)
        If messageId = vbNullString Then
            WScript.Echo "Error with Retrieve method"
            WScript.Quot
        End If
                
        If  Not IsMessageIdExisted( messageId ) Then
            emailContent = oPop3.Retrieve(i)
            If emailContent = vbNullString Then
                WScript.Echo "Error with Retrieve method"
                WScript.Quit()  
            End If

            oMsg.RawContent = emailContent
            WScript.Echo oMsg.GetFromAddress()
            WScript.Echo oMsg.GetSubject()
            'you can save this email to disk by oMsg.ExportFile     
        End If
        
        newmessageIds = newmessageIds & "<" & messageId & ">" & Chr(13) & Chr(10)   
    Next

    oPop3.Close
End Sub

Dim args, info
Set args = WScript.Arguments
If args.Count < 3 Then
  info =  "Usage: retr.vbs [pop3server] [user] [password]" & Chr(13) & Chr(10)
  info = info & " eg: retr.vbs mail.adminsystem.net test@adminsystem.net test"
  WScript.Echo info
  WScript.Quit
End If


Call LoadMessageIds
Call Retrieve(args(0), args(1), args(2))
Call SaveMessageIds

To learn more detail, please refer to VBSAMPLE4, VCSAMPLE1, POP3QUEUE samples in ANPOP installation package.

2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.