ANPOP Developers Center > Using ANPOP in ASP/ASP.NET

You should download the anpop installer and install it on your machine at first. If you want to distribute or deploy anpop without anpop installer, please click here to learn more.

Set up your IIS for ASP/ASP.NET application

Firstly, you need to install ANPOP on your server. Secondly, create a virtual directory for your asp/asp.net application on IIS. Please note that you should assign this virtual directory with permission to run script. For ASP.NET, you have to create a sub-directory named "bin" under this virtual directory, and copy "ANPOPLib.dll" to "bin" directory. You can find "ANPOPLib.dll" in "DOTNET Assembly" sub-directory of ANPOP installation directory("C:\Program Files\AdminSystem.NET\ANPOP\DOTNET Assembly"). It is a run-time library of ANPOP for .NET. If the .aspx is under IIS root, so you should create "bin" under "C:\Inetpub\wwwroot", and copy ANPOPLib.dll to "C:\Inetpub\wwwroot\bin"

Create object instance

Now, we can use ANPOP in ASP/ASP.NET more easily. The following code demonstrates how to create object instances in ASP/ASP.NET.

[ASP]
<%
Dim oPop3, oMsg, oStore
Set oPop3 = Server.CreateObject("ANPOP.POPMAIN")
Set oMsg = Server.CreateObject("ANPOP.POPMSG")
Set oStore = Server.CreateObject("ANPOP.MSGSTORE")
%>
[ASP.NET/VB]
<%@ Page language="VBScript" AutoEventWireup="true" aspcompat=true%>
<%@ Import Namespace="ANPOPLib"%>
<%
Sub Page_Load( sender As Object ,  e As System.EventArgs )
  Dim oPop3 As POPMAINClass
  Dim oMsg As POPMSGClass 
  Dim oStore As MSGSTOREClass
  oPop3 = New POPMAINClass()
  oMsg = New POPMSGClass()
  oStore = New MSGSTOREClass()
End Sub
%>
[ASP.NET/C#]
<%@ Page language="C#" AutoEventWireup="true" aspcompat=true%>
<%@ Import Namespace="ANPOPLib"%>
<%
private void Page_Load( object sender,  System.EventArgs e )
{ 
  POPMAINClass oPop3 = new POPMAINClass();
  POPMSGClass oMsg = new POPMSGClass();
  MSGSTOREClass oStore = new MSGSTOREClass();
}
%>

How to retrieve email from pop3 server

Now, we can retrieve email in ASP/ASP.NET with the following code.

[ASP]
<%
Dim oPop3, oMsg, oStore
Set oPop3 = Server.CreateObject("ANPOP.POPMAIN")
Set oMsg = Server.CreateObject("ANPOP.POPMSG")
Set oStore = Server.CreateObject("ANPOP.MSGSTORE")

Dim nRet, i, nCount
nRet = oPop3.Connect( "mail.adminsystem.net", "test@adminsystem.net", "test")
If nRet <> 0 Then
  Response.Write "Error with Connect server"
  Response.End
End If

nCount = oPop3.GetTotalOfMails()
If nCount = -1 Then
  Response.Write "Error with GetTotalOfMails method"
  Response.End
End If

Dim emailContent, vbNullString
vbNullString = "" 'asp doesn't support vbNullString, then define it.
For i = 1 To nCount
  emailContent = oPop3.Retrieve(i)
  If emailContent = vbNullString Then
    Response.Write "Error with Retrieve method"
    Exit For
  End If
  
  oMsg.RawContent = emailContent
  If oMsg.ExportFile( "c:\test" & i & ".eml" ) <> Then
    Response.Write "Error with ExportFile method"
    Exit For
  End If
Next
oPop3.Close
%>
[ASP.NET/C#]
<%@ Page language="C#" AutoEventWireup="true" aspcompat=true%>
<%@ Import Namespace="ANPOPLib"%>
<%
private void Page_Load( object sender,  System.EventArgs e )
{ 
  POPMAINClass oPop3 = new POPMAINClass();
  POPMSGClass oMsg = new POPMSGClass();
  try
  {
     int nRet = 0, nCount = -1, i = 0;
     nRet = oPop3.Connect( "mail.adminsystem.net", "test@adminsystem.net", "test" );
     if( nRet != 0 )
	   throw new Exception( "Error with Connect method" );
	 
	 nCount = oPop3.GetTotalOfMails()
	 if( nCount == -1 )
	   throw new Exception( "Error with GetTotalOfMails method" );
	 
	 string emailContent = "";
	 for( i = 1; i <= nCount; i++ )
	 {
	   emailContent = oPop3.Retrieve( i );
	   if( emailContent == null )
	     throw new Exception( "Error with Retrieve method" );
	   
	   oMsg.RawContent = emailContent;
	   if( oMsg.ExportFile( String.Format( "c:\\test{0}.eml", i )) != 0 )
	     throw new Exception( "Error with ExportFile method" );
	 }
  }
  catch( Exception e )
  {
    Response.Write( e.Message );
  }
}
%>

More summary

If you develop ASP.NET application with Visual Studio.NET, please refer to the corresponding tutorials. You may also refer to ASPSAMPLE1, ASPSAMPLE2, ASP.NET and POP3QUEUE samples in ANPOP installation package.

Related links

Retrieve Email in Background
Security Context in ASP/ASP.NET

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.



2001-2010 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map