The following article will describe how to use aspNetIMAP from Visual Studio .NET using C#. This brief tutorial assumes you have IIS installed locally on your machine, and have downloaded and installed aspNetIMAP from www.aspNetIMAP.com . For other examples, be sure to visit www.aspNetIMAP.com/examples.aspx
Using Visual Studio .NET (VS.NET ) We will create a single webform (imaptest.aspx) and send out an email.
1. Launch Visual Studio .NET
2. From the main menu, select the File | New | Project command.
3. The New Project dialog box appears. Under Project Types, Select Visual C# Projects. Under Templates, select ASP.NET Web Application. In the Location textbox, enter http://localhost/aspNetIMAPTestCS
There are two ways to set a reference in VS.NET . Because aspNetIMAP is installed in the GAC (Global Assembly Cache) you can set a reference to the GAC copy, or you can upload the aspNetIMAP.dll and the aspNetMime20.dll to the project and set a reference to the uploaded copies. We will upload a copy, and set a reference to the uploaded copy. This method will promote XCOPY deployment. (A comment about licensing: Please be sure you are compliant with your licenses. Check http://www.aspNetIMAP.com/licen.aspx for more information.)
1. In the Solution Explorer, right-click the project name, aspNetIMAPTest, and select the Add | Add Existing Item command.
2. The Add Existing Item dialog box appears. Under Files of Type, select All Files(*.*). Navigate to the aspNetIMAP install directory. By default, this directory is C:\Program Files\AdvancedIntellect\aspNetIMAP. Select the aspNetIMAP.dll and the aspNetMime20.dll The Add Existing Item dialog box closes, and the two dlls were uploaded to the root directory of your project.
3. In the Solution Explorer, right-click the project name aspNetIMAPTestCS and select Add Reference. The Add Reference dialog box appears. Click the Browse button. The Select Component dialog box appears. Select the aspNetIMAP.dll and aspNetMime20.dll files. The Select Component dialog box closes. Click OK. The Add Reference dialog box closes, and a reference is set to aspNetIMAP and aspNetMime.
Now that we have a reference set, let's create a test page to check aspNetIMAP.
1. In the Solution Explorer, right-click the project name aspNetIMAPTestCS, and select Add | Add Webform. The Add New Item dialog box appears. In the Name textbox, enter, imaptest.aspx.
2. In the Solution Explorer, right-click imaptest.aspx, and select View Code.
3. At the top of the page, add the following using statement.
using aspNetIMAP;
4. Under the Page_Load function, replace the line
// Put user code to initialize the page here
So the entire Page_Load method is as follows:
private void Page_Load(object sender, System.EventArgs e) { //set various properties IMAP4 imap = new IMAP4( "127.0.0.1" ); imap.Username = "dave@blah.com"; imap.Password = "test"; //create an in memory log for testing purposes imap.Logger = new IMAPLog(); imap.Logger.InMemory = true; DataGrid dgInbox = new DataGrid(); try { imap.Login(); //list all of the folders MailFolderCollection mfc = imap.FolderList(); //convert the folders to a DataSet dgInbox.DataSource = mfc.ToDataSet(); dgInbox.DataBind(); } catch(Exception ex) { Response.Write( "<pre>" + ex.ToString() + "</pre>" ); } finally { imap.Disconnect(); } //write out the log Literal ImapLog = new Literal(); ImapLog.Text = "IMAP Log<pre>" + imap.Logger.ToString() + "</pre>"; //add the datagrid and the log to the page Page.Controls.Add( dgInbox ); Page.Controls.Add( ImapLog ); }
You will need to replace the following properties with values that will work with your system:
IMAP4 imap = new IMAP4( "127.0.0.1" );
imap.Username = "dave@blah.com";
imap.Password = "test";
Lets compile and test the page.
1. In the Solution Explorer, right-click the project name, aspNetIMAPTestCS, and select Build. The project will be compiled.
2. In the Solution Explorer, right-click imaptest.aspx and select View in Browser.
3. The listing of mail folders will appear, in a datagrid.
That's all there is to using aspNetIMAP from Visual Studio .NET. In these few simple steps you were able to create a project, set a reference to aspNetIMAP, and download a list of folders. For more questions or comments, feel free to write support@advancedIntellect.com