9

Thats what I am using to read e-mail using C#:

outLookApp.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx);
            Outlook.NameSpace olNameSpace = outLookApp.GetNamespace("mapi");

olNameSpace.Logon("xxxx", "xxxxx", false, true);
Outlook.MAPIFolder oInbox  = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items oItems  = oInbox.Items;
MessageBox.Show("Total : " + oItems.Count); //Total Itemin inbox
 oItems = oItems.Restrict("[Unread] = true");
 MessageBox.Show("Total Unread : " + oItems.Count); //Unread Items
 Outlook.MailItem oMsg;


 Outlook.Attachment mailAttachement;
 for (int i = 0; i < oItems.Count; i++)
 {
     oMsg = (Outlook.MailItem)oItems.GetFirst();

     MessageBox.Show(i.ToString());

    MessageBox.Show(oMsg.SenderName);
    MessageBox.Show(oMsg.Subject);
    MessageBox.Show(oMsg.ReceivedTime.ToString());
    MessageBox.Show(oMsg.Body);

The problem that I am facing is this application only works if the Outlook is open on the machine. If Outlook is closed it throws an exception:

The server is not available. Contact your administrator if this condition persists.

Is there anyway I can read e-mail with Outlook open?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

6 Answers6

2

You'll likely run into this when Outlook is closed.

Also following this tutorial will ensure you're doing all the right steps part and parcel.

Best of luck!

Community
  • 1
  • 1
Mat Nadrofsky
  • 8,289
  • 8
  • 49
  • 73
1

This is kind of an old question, but I am going to answer it since I struggled with the same issue for a long time and the previous answers on this page did not really help me.

I had to write a program and use outlook to send an email on different machines with different UAC-levels and this is what I came up with after a long time.

using Outlook = Microsoft.Office.Interop.Outlook;

// Create the Outlook application.
Outlook.Application oApp = null;

// Check whether there is an Outlook process running.
int outlookRunning = Process.GetProcessesByName("OUTLOOK").Length;
if (outlookRunning > 0)
{
    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
    try
    {
        oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
    }
    catch (Exception)
    {
        oApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")) as Outlook.Application;
    }
    finally
    {
        // At this point we must kill Outlook (since outlook was started by user on a higher prio level than this current application)
        // kill Outlook (otherwise it will only work if UAC is disabled)
        // this is really a kind of last resort
        Process[] workers = Process.GetProcessesByName("OUTLOOk");
        foreach (Process worker in workers)
        {
            worker.Kill();
            worker.WaitForExit();
            worker.Dispose();
        }
    }
}
else
{
    // If not, create a new instance of Outlook and log on to the default profile.
    oApp = new Outlook.Application();
    Outlook.NameSpace nameSpace = oApp.GetNamespace("MAPI");
    try
    {
        // use default profile and DO NOT pop up a window
        // on some pc bill gates fails to login without the popup, then we must pop up and lets use choose profile and allow access
        nameSpace.Logon("", "", false, Missing.Value);
    }
    catch (Exception)
    {
        // use default profile and DO pop up a window
        nameSpace.Logon("", "", true, true);
    }
    nameSpace = null;
}

// Done, now you can do what ever you want with the oApp, like creating a message and send it
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
theAlse
  • 5,577
  • 11
  • 68
  • 110
0

Are you sure you want to use Outlook as a proxy?

people seems to deal low level with such a task in C# (surprising there isn't any built-in component in the framework...)

Concerning Mat's response, Redemption is indeed a fine product (used it to parse mails upon arrival in outlook), but I doubt it can work without outlook running.

Vinzz
  • 3,968
  • 6
  • 36
  • 51
0

I would personally not use Outlook as a proxy. If you're trying to ultimately monitor an Exchange store, then I'd use WebDav. Your Exchange server must support it - but if it does, it's a simple XML API. Well, the API bit is simple, but the XML is quite convoluted. But once you've encapsulated this in a bit of code, it's a doddle to use.

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
0

Use the Redemption COM library for your code.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Use a MAPI client to retrieve the emails and a MIME decoder to read them. Both exists in the lumisoft framework:

http://www.lumisoft.ee/lswww/download/downloads/Net/

jgauffin
  • 99,844
  • 45
  • 235
  • 372