Question

I have a program that needs to connect to my companies Exchange 2003 server and look through a specific folder in the Public Folders. I will eventually be looping through this folder and scanning (using OCR) each email so I can parse the information for our database.

However, I lack the knowledge of how to even start doing this. I have see things about WebDAV on the net and that seems to be the only way to do this in C# and on an Exchange 2003 server. If someone can please let me know of an easier way that'd be nice.

If not, then please give me some simple explanation of WebDAV. The stuff I've seen online about it makes it seem a LOT more complicated than I thought it should be. At least for the task I am trying to do.

Was it helpful?

Solution

First add a reference to Microsoft.Office.Interop.Outlook, then add these using statements

using Microsoft.Office.Interop.Outlook;
using _Application = Microsoft.Office.Interop.Outlook._Application;
using Outlook = Microsoft.Office.Interop.Outlook;

Then add this

//declare Outlook application             
_Application objOutlook = new Outlook.Application();  //create it
_NameSpace objNs = objOutlook.Session; //create new session

MAPIFolder oPublicFolders = objNs.Folders["Public Folders"];
MAPIFolder oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
Items itms = oAllPublicFolders.Items;

foreach (MailItem item in itms)
{
     GetNewMailItem(objOutlook, objContacts, item);
}

Marshal.ReleaseComObject(objOutlook); //release outlook com object

This next piece is just what I did, please change all of this code to fit your needs.

private static void GetNewMailItem(_Application objOutlook, MAPIFolder objContacts, MailItem item)
{
     if(item.ReceivedTime.Date == DateTime.Now.Date.AddDays(-1) || item.UnRead)
     {
          if (item.Attachments.Count > 0)
          {
               var attachments = item.Attachments;
               foreach (Attachment attachment in attachments)
               {
                    if(attachment.Type == OlAttachmentType.olEmbeddeditem)
                    {
                         ProcessEmbeddedEmailAttachment(attachment, objOutlook, objContacts);
                    }
                    else if (attachment.FileName.EndsWith(".doc") || attachment.FileName.EndsWith(".docx"))
                    {
                         ExtractAttachment(attachment);
                         item.UnRead = false;
                    }
               }
          }
     }
}

I am leaving out the rest of the code because it was specific to my processing.

OTHER TIPS

MAPI is similar (you will need to install CDO 1.21 - later versions of Exchange don't use CDO any more and instead use Exchange Web Services)

Add a reference to MAPI to your project. Code is similar to the other answer:

var session = new Session();

// Need this to pass null values to MAPI calls - can't remember why exactly - YMMV
object mObj = System.Reflection.Missing.Value;
session.Logon(MailProfileName, mObj, false, mObj, mObj, mObj, mObj);

// Get the inbox for now for the current user
inbox = (Folder)session.GetDefaultFolder(1);

// more processing etc...

Obviously have a look at the CDO reference - the code above was originally used to process some mail items from the inbox for the selected profile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top