Preview Email on POP3 Server


Usually, we need to use Retrieve method of POPMAIN object to retrieve whole email to local disk and use POPMSG to parse it and get subject, body text, from and etc. However, sometimes we only need to know the sender, recipient and subject, then we can decide whether download the email from server by the sender, subject. In this case, we needn't to download the whole email with Retrieve method. You can use GetMsgHeader method of POPMAIN object to download the header of email and get the subject, sender and recipients. With GetMsgHeader method, we can preview email on POP3 server easily.

The following code demonstrates how to preview emails on the POP3 server with VBScript.

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

Dim oPop3, oMsg, nRet, i, nCount, nSize, headerContent

WScript.Echo "Connecting " & args(0) & " ..."

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

nRet = oPop3.Connect(args(0), args(1), args(2))
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
    nSize = oPop3.GetMsgSize(i)
    If nSize = -1 Then
        WScript.Echo "Error with GetMsgSize method"
        Exit For
    End If

    headerContent = oPop3.GetMsgHeader(i)
    If headerContent = vbNullString Then
        WScript.Echo "Error with GetMsgHeader method"
        Exit For
    End If
    
    oMsg.RawContent = headerContent
    info = i & ": size: " & nSize & " bytes, date: " & oMsg.GetDate() & Chr(13) & Chr(10)
    info = info & "From: " & oMsg.GetFrom() & "<" & oMsg.GetFromAddress() & ">" & Chr(13) & Chr(10)
    info = info & "Subject: " & oMsg.GetSubject() & Chr(13) & Chr(10)
    WScript.Echo info
Next

oPop3.Close
WScript.Quit

Note: With GetMsgHeader method, you can't get the body text and attachments from the message. If you want to get the body text and attachments, you should download the email you are interested in by Retrieve method after previewing it.


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