0
votes

I am trying to create a service which will run as a background service on a server. This service will parse email as soon as it enters the Inbox.

We have one email account [email protected]. We are using Outlook to check the emails. It is a service so Outlook won't be running all the time on the server. I want to parse the email automatically for this account. This account is not my email account. I am using Microsoft.Office.Interop.outlook in C# program.

This program is running but it is parsing email from my email inbox. I don't know how to specify specific email to parse the Inbox. Need to know the event which triggers as soon as new mail arrives. My program parsed half the emails from my inbox but after that it is throwing object null reference error.

static void Main(string[] args)
    {
        Microsoft.Office.Interop.Outlook.Application myApp=new Microsoft.Office.Interop.Outlook.Application();
        try
        {
            String Subject, Body, Createdate, Sender = null; 
            Microsoft.Office.Interop.Outlook.NameSpace mapinamespace = myApp.GetNamespace("MAPI");

            mapinamespace.Logon(null, null,true,true);
            Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapinamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            Microsoft.Office.Interop.Outlook.Accounts accounts = myApp.Session.Accounts;
            try
            {
                foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
                {
                    if (account.SmtpAddress == "[email protected]")
                    {
                        Console.Write(account);
                    }
                    else
                    {
                        Console.Write("Not found ---");
                    }
                }
            }
            catch{
            throw new System.Exception(string.Format("No account with amtp:{0} exists!"));}

            foreach (object item in myInbox.Items)
            {
                try
                {
                    var mail = item as MailItem;
                    if (mail != null)
                    {
                        //Creation date
                        Createdate = mail.CreationTime.ToString();
                        Console.WriteLine("CreationTime---" + Createdate);
                        //Grab the senders address
                        Sender = mail.SenderEmailAddress.ToString();
                        Console.WriteLine("Sender's E-mail---" + Sender);
                        //grab the Subject
                        Subject = mail.Subject.ToString();
                        Console.WriteLine("Subject--" + Subject);
                        //Grab the body
                        Body = mail.Body.ToString();
                        Console.WriteLine("Body---" + Body);

                        //Grab the Attachment

                    }
                    else
                    {
                        Console.Write("Error in mail---");
                    }
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    Console.Write(e);
                }
            }
        }
        catch (System.Runtime.InteropServices.COMException e)
        {
            Console.Write(e);
        }



    }
2

2 Answers

0
votes

I am trying to create a service which will run as a background service on server.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

You can read more about that in the Considerations for server-side Automation of Office article.

Anyway, it looks like you are interested in the NewMailEx event of the Application class. Also I'd suggest reading the following series of articles:

0
votes

As Eugene mentioned, you cannot use Outlook (or any other Office app) in a service.

For Exchange mailboxes, you can use EWS (which is HTTP based) instead of the Outlook Object Model.

You can also use Extended MAPI (C++ or Delphi) or Redemption (any language) to access Outlook data in a service.