ANPOP Developers Center > Using ANPOP in Visual Basic
Add reference of ANPOP to your project
First of all, make sure ANPOP has been installed on your machine. Now start to create a standard visual basic project: select menu "Project" -> "References", checked "ANPOP POP3 COMPONENT BUILD" and click "OK".
Using ANPOP in your project
The following code demonstrates how to declare and define objects in your project.
Option Explicit private m_oPop3 As ANPOPLib.POPMAIN private m_oMsg As ANPOPLib.POPMSG private m_oStore As ANPOPLib.MSGSTORE Sub Form_Load() Set m_oPop3 = New ANPOPLib.POPMAIN Set m_oMsg = New ANPOPLi.POPMSG Set m_oStore = New ANPOPLib.MSGSTORE End Sub
Although you can create new object instance each time, we recommend to declare object as member variable and create it in Form_Load subroutine so that you can reuse those member variables again without re-create it.
How to retrieve and parse email
The following code demonstrates how to retrieve and parse email
Sub Retrieve( pop3Server As String, pop3User As String, pop3Password As String )
Dim oPop3 As ANPOPLib.POPMAIN
Dim oMsg As ANPOPLib.POPMSG
Dim i, nRet, nSize, nCount As Integer
Dim emailContent, messageId, err As String
Dim emailSubject, emailBody As String
Set oPop3 = new ANPOPLib.POPMAIN 'Create object instance
Set oMsg = new ANPOPLib.POPMSG
err = ""
nRet = oPop3.Connect( pop3Server, pop3User, pop3Password ) 'Connect pop3 server
If nRet <> 0 Then
err = "error with connecting server"
goto ErrorHandler
End If
nCount = oPop3.GetTotalOfMails() 'Get total count of emails
If nCount = -1 Then
err = "error with GetTotalOfMails"
goto ErrorHandler
ElseIf nCount = 0 Then
err = "no email"
goto ErrorHandler
End If
For i = 1 To nCount
nSize = oPop3.GetMsgSize(i) 'Get email size
If nSize = -1 Then
err = "error with GetMsgSize"
goto ErrorHandler
End If
messageId = oPop3.GetMsgID(i) 'Get message-id
If messageId = vbnullstring Then
err = "error with GetMsgId"
goto ErrorHandler
End If
emailContent = oPop3.Retrieve(i) 'Retrieve email
If emailContent = vbnullstring Then
err = "error with Retrieve"
goto ErrorHandler
End If
oMsg.RawContent = emailContent
emailSubject = oMsg.GetSubject() 'parse email subject
emailBody = oMsg.GetBodyText() 'parse email body
nRet = oMsg.ExportFile( "c:\pop3Test_" & i & ".eml") 'Save email
If nRet <> 0 Then
err = "error with ExportFile"
goto ErrorHandler
End If
If oPop3.Delete(i) <> 0 Then 'Delete email from server
err = "error with Delete"
goto ErrorHandler
End If
Next
ErrorHandler:
Call oPop3.Close() 'Close connection
Set oPop3 = Nothing
Set oMsg = Nothing
End Sub
Asynchronous mode and event driving
If you want to use POPMAIN in asynchronous mode, you MUST declare it as a member variable in your project with "WithEvents" key word.
Option Explicit private WithEvents m_oPop3 As ANPOPLib.POPMAIN private m_oMsg As ANPOPLib.POPMSG private m_oStore As ANPOPLib.MSGSTORE Sub Form_Load() Set m_oPop3 = New ANPOPLib.POPMAIN Set m_oMsg = New ANPOPLi.POPMSG Set m_oStore = New ANPOPLib.MSGSTORE m_oPop3.Asynchronous = 1 End Sub
Note*: You SHOULD NOT invoke Connect method any more while POPMAIN is connecting with pop3 server, otherwise this connection would be terminated automatically.
Samples
Please refer to VBSAMPLE1, VBSAMPLE2, VBSAMPLE3 and VBSAMPL4 in ANPOP installation package.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your smtp server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|