Thursday, December 17, 2015

ASP.Net IIS7: The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.


The below steps shows how to register ASP.Net with IIS i.e. installing the ASP.Net in IIS so that ASP.Net pages can be served from IIS server. If ASP.Net is not registered with IIS then when accessing ASP.Net Website you will get the following error.

HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Once the ASP.Net is registered,IIS will show ASP.Net 4.0 Application pools.

The first thing you should do is installing the .Net Framework 4.0.
1.Install the necessary .Net framework in your system.
2.Once the .Net Framework is installed,you can look for the aspnet_regiis.exe file for registering the .Net Framework in IIS server.
If your windows is installed in c drive you could find the aspnet_regiis.exe at the following locations.

64 BIT
C:\Windows\Microsoft.NET\Framework64


Now based on whether your System is 32 BIT or 64 BIT and the .Net Framework version you want to register, you need to get into the .Net Framework Folder.In my case the Windows is 64 BIT and I need to register .Net Framework64 I am looking for the aspnet_regiis.exe in the following location
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\


3.Registering the Asp.Net with IIS Server.
Start the command prompt by typing cmd at the Run command and then execute the command prompt with Administrator rights by right clicking and choosing Run as administrator from the context menu.
Then on the command prompt you need to navigate to the Directory that has the aspnet_regiis.exe file for that you need to type.
CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319
And then you need to type the following command and hit enter to register ASP.Net with IIS.
        aspnet_regiis -i






Once the ASP.net is installed in IIS,you will see ASP.Net 4.0 Application pools in the IIS server.




Here you are done with registering ASP.Net with IIS.You can register other versions in similar way.

Wednesday, December 9, 2015

Accessing Unread Mails from Outlook through C# Code

This example shows how to get unread mails from outlook through C# code.

Getting the collection of Unread mails using Restrict() method.

The following demonstrates how to retrieve data from items within an Outlook folder (called "IN" under the Inbox folder) using .NET.

First add a reference to the Outlook COM object your project.
  1. In VS.NET right click on References and choose Add Reference.
  2. Select the COM tab
  3. Choose "Microsoft Outlook 15.0 Object Library" (this is for MS Office 2013 ) and click Select.
  4. Click OK.
            using Microsoft.Office.Interop.Outlook;

            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace namespce = null;
            Microsoft.Office.Interop.Outlook.MailItem item = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
            Microsoft.Office.Interop.Outlook.Items inboxItems = null;
            Microsoft.Office.Interop.Outlook.Items restrictedItems = null;


            try
            {
                app = new Microsoft.Office.Interop.Outlook.Application();
                namespce = app.GetNamespace("MAPI");
                namespce.Logon(null, null, false, false);

                inboxFolder = namespce.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["IN"]; //folder.Folders[1]; also works
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

                foreach (Folder folder in inboxFolder.Folders)
                {

                    if (folder.Name == "IN")
                    {
                        Console.WriteLine(folder.Name);
                        inboxItems = folder.Items;
                        restrictedItems = inboxItems.Restrict("[Unread]=true");
                        for (int i = 1; i <= restrictedItems.Count; i++)
                        {
                            item = (Microsoft.Office.Interop.Outlook.MailItem)folder.Items[i];
                            Console.WriteLine("Subject: {0}", item.Subject);
                        }
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                namespce = null;
                app = null;
                inboxFolder = null;
            }

We can also use Find() and FindNext() of outlook items instead of Restrict() for reading unread mails.
The Find() and FindNext() methods of the Outlook Items object are faster than the Restrict ()method, but only when working with a small number of items. The Find() method returns a single item based on the filter, but if we want to retrieve the next mail item that matches the filter we would have to call the FindNext() method.

We can loop through unread mails returned by the Folder.GetTable() method.The GetTable() method can be found on the outlook folder object and returns a Table object.Each item returned inside the Table object only contains a certain number of fields ,we can load the items returned using the GetItemFromID() method.


Accessing mail from Outlook through C# .NET

This example shows how to get the mails from outlook through C# code.

The following demonstrates how to retrieve data from items within an Outlook folder (called "IN" under the Inbox folder) using .NET.First add a reference to the Outlook COM object your project:


  1. In VS.NET right click on References and choose Add Reference.
  2. Select the COM tab
  3. Choose "Microsoft Outlook 15.0 Object Library" (this is for MS Office 2013 ) and click Select.
  4. Click OK.
            using Microsoft.Office.Interop.Outlook;

            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace namespce = null;            
            Microsoft.Office.Interop.Outlook.MailItem item = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
     
              try
            {
                app = new Microsoft.Office.Interop.Outlook.Application();
                namespce = app.GetNamespace("MAPI");
                namespce.Logon(null, null, false, false);

                inboxFolder = namespce.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["IN"]; 
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

                foreach (Folder folder in inboxFolder.Folders)
                {
                    
                    if (folder.Name == "IN")
                    {
                        Console.WriteLine(folder.Name);
                        for (int i = 1; i <= folder.Items.Count; i++)
                        {
                            item = (Microsoft.Office.Interop.Outlook.MailItem)folder.Items[i];                                  
                            Console.WriteLine("Subject: {0}", item.Subject);                            
                        }
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                namespce = null;
                app = null;
                inboxFolder = null;
            }