Create Your Own PageGetter Using Outlook and C# Programming

Introduction

PageGetter is one helpful feature that most corporate professionals would like to have. At times when certain Websites are blocked, or in the case of no internet connectivity, page getters serve a mammoth boon.

Well, this article does not compare itself to PageGetter’s available in the market, this just overviews the use of the .NET Framework to build an easy one in less than an hour and serve yourself as “Page Getter” to your friends.

Please Note: This article assumes that you have an Outlook profile running on a server.

Pre-requisites


  • Microsoft ActiveX Data Objects 2.7 Library

  • Microsoft CDO for Windows 2000 Library (installed with Outlook)

  • Microsoft Outlook 11.0 Object Library

  • Microsoft Visual J# .NET Class Library

Overview

This application searches the Outlook Default Profile account for any emails that has a URL in the subject and the body of the email starting with the word “url” (this acts a filter). So any mails found with such a pattern, it checks for the validity of the URL requested and downloads the file in .mht format and zips and sends it to the user who requested it. If the URL requested is invalid, then it sends an email to the user saying it is a “bad url”.

The application also runs itself on a specified interval (specified through the pageGetter.config file).

This application is a simple idea that can be enriched with WPF UI, WCF framework and much more. It uses .NET controls that come with Microsoft Visual Studio by default.

Description

The application uses two other classes for its functioning.


  • Zipper.cs: Has methods to compress a file/directory, built using the MS Visual J# library , available in 1.1

  • Helper.cs: Methods to check a URL, download a file in .mht format, send mail, and are available here.

This article starts off with a reference to the Outlook 11.0 Library.

To date, the functionality of Outlook is exposed through a COM-based in-process server (msoutl.olb). When .NET developers wish to interact with these COM types, therefore, you will need to do so through the interoperability layer. Microsoft Corporation has already supplied an ‘official’ interop assembly (a.k.a., the primary interop assembly) which ships with Outlook 2003.

This assembly has been strongly named, and resides in the Global Assembly Cache under the name Microsoft.Office.Interop.Outlook.dll. To reference this assembly from Microsoft Visual Studio .NET 2003, access the COM tab from the “Add References” dialog and select “Microsoft Outlook 11.0 Object Library”.



Figure 1

Outlook.Application   outAp  = new Outlook.Application();
Outlook.NameSpace   outNS    = outapp.GetNamespace(“MAPI”);
Outlook.MAPIFolder    MyBox   =  outNS.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Ref: http://msdn2.microsoft.com/en-us/library/aa289167(vs.71).aspx

And followed by a call to GetMailList() function which does the whole work.

Please note: The following message might appear as a custom made program is trying to access Outlook. Although intrusive to the end user, this behavior is by design. It is one of the security features built in 11.0. If there is any simple way of avoiding it other than using redemption, please let me know.



Figure 2

This function loops through all mails in the inbox, looking for mails that start with the word “url” in their body. In case of finding such a mail, it checks the URL, and if valid downloads the file in the .mht format using the SaveWebPageToMHTFile() that is present in the Helper Class itself.


System.Net.WebClient client = new System.Net.WebClient();
byte [] bytedata = client.DownloadData(url);
client.DownloadFile(url,sPath);

The above code downloads as normal htm files. Since .mht files are good in the sense, that they are downloaded with all supporting files (css, images etc) they are always a better option.

So we used the ADO and CDO objects to create a function SaveWebPageToMHTFile()


//Saves the url to a Archive File (.mht)

public static bool SaveWebPageToMHTFile( string url, string filePath)
{

CDO.Message msg = new CDO.MessageClass();
ADODB.Stream stm = null ;

msg.MimeFormatted =true;   
msg.CreateMHTMLBody
(url,CDO.CdoMHTMLFlags.cdoSuppressNone, “” ,”” );
stm = msg.GetStream();
stm.SaveToFile
(filePath,ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
msg=null;
stm.Close();

}


Since .mht files are naturally very big to their .htm counterparts, I thought zipping them would be handy.

The requested file is saved in a folder specified in the config file through the FileName parameter. Ensure that you write permission to this directory.

This application uses the Visual J# library that comes with .NET framework 1.1 to zip the file. Then zips and sends the zipped version to the requested user and keeps updating the listview on the main form for the status of the mails sent.

For the context menu on the listview, it just uses an image which launches the non rectangular about form.

The about form is created using the trivial way of achieving non-rectangular forms.

And the rest of the code is self-explanatory.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read