Пример #1
0
        //Search mails in a specified account
        public static MAPIFolder SearchTargetMailAccount(OutlookApp outlookApp, string accountName)
        {
            _NameSpace ns = outlookApp.OutlookAppInstance.GetNamespace("MAPI");

            ns.Logon(null, null, false, false);
            MAPIFolder folder = null;

            try
            {
                Folders folders = outlookApp.OutlookAppInstance.Application.Session.Folders;

                foreach (MAPIFolder item in folders)
                {
                    if (item.Name.Contains(accountName))
                    {
                        folder = item;
                        break;
                    }
                }
            }

            catch (System.Exception ex)
            {
                throw new System.Exception(string.Format("Error found when searching the target mail account {0}. The exception message is: {1}", accountName, ex.Message));
            }
            return(folder);
        }
Пример #2
0
        protected void OpenMessage(string itemId)
        {
            Application app  = null;
            _NameSpace  ns   = null;
            MailItem    item = null;

            app = new Application();
            ns  = app.GetNamespace("MAPI");
            ns.Logon(null, null, false, false);
            item = ns.GetItemFromID(itemId);
            item.Display();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Application app         = null;
            _NameSpace  ns          = null;
            MailItem    item        = null;
            MAPIFolder  inboxFolder = null;
            MAPIFolder  subFolder   = null;

            try
            {
                app = new Application();
                ns  = app.GetNamespace("MAPI");
                ns.Logon(null, null, false, false);
                inboxFolder = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                subFolder   = inboxFolder.Folders["TEST"];
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
                for (int i = 1; i <= subFolder.Items.Count; i++)
                {
                    item = (MailItem)subFolder.Items[i];
                    Message message = new Message(item);
                    string  json    = JsonConvert.SerializeObject(message);
                    IndexLowLevel(json);
                }
                //item = ns.GetItemFromID("000000006F8917C81DA08C409D3B1B49454F46C90700C6936CD0FB24CA4482A4BEEF45C1DF1E0000000002100000C6936CD0FB24CA4482A4BEEF45C1DF1E000000001C020000");
                //item.Display();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadLine();
            }
            finally
            {
                ns          = null;
                app         = null;
                inboxFolder = null;
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("*******************************************************");
            Console.WriteLine("Invalid Email Address identification Process Started...");
            Console.WriteLine("*******************************************************\n\n");

            //Intialize variables
            Application outlookApplication    = null;
            _NameSpace  outlookNameSpace      = null;
            MAPIFolder  inboxFolder           = null;
            MAPIFolder  workingFolder         = null;
            MAPIFolder  processedReportFolder = null;
            MAPIFolder  processedMailFolder   = null;
            MAPIFolder  exceptionReportFolder = null;
            MAPIFolder  exceptionMailFolder   = null;
            int         totalOutlookItems;
            int         totalMailItems;
            int         totalReportItems;

            List <MailItem>   MailItemList   = new List <MailItem>();
            List <ReportItem> ReportItemList = new List <ReportItem>();

            Console.WriteLine("Required variables Intialized...\n\n");



            try
            {
                //Logon to outlook Application using default profile
                outlookApplication = new Application();
                outlookNameSpace   = outlookApplication.GetNamespace("MAPI");
                outlookNameSpace.AddStore("");
                outlookNameSpace.Logon(null, null, false, false); //first value null as Default outlook profile


                //Set working folders
                inboxFolder   = outlookNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                workingFolder = inboxFolder.Folders["Working Folder"];                                        //folder.Folders[1]; also works

                processedReportFolder = workingFolder.Folders["Done Folder"].Folders["Report Items Folder"];  //assuming folders are in working folder
                processedMailFolder   = workingFolder.Folders["Done Folder"].Folders["Mail Items Folder"];    //assuming folders are in working folder

                exceptionReportFolder = workingFolder.Folders["Error Folder"].Folders["Report Items Folder"]; //assuming folders are in working folder
                exceptionMailFolder   = workingFolder.Folders["Error Folder"].Folders["Mail Items Folder"];   //assuming folders are in working folder

                // Displaying All Folders
                Console.WriteLine("Inbox Folder: {0},\n Working Folder: {1},\n Processed Folder: {2} & {3},\n Exception Folder: {4} & {5}", inboxFolder.Name, workingFolder.Name, processedReportFolder.Name, processedMailFolder.Name, exceptionReportFolder.Name, exceptionMailFolder.Name);


                totalOutlookItems = workingFolder.Items.Count;
                Console.WriteLine("\tComplete number of Items found: {0}", totalOutlookItems.ToString());


                #region doingStuff

                // only run when some outlook items are found
                if (totalOutlookItems > 0)
                {
                    //Saves List of all Mail Items
                    foreach (MailItem myitem in workingFolder.Items.OfType <MailItem>())
                    {
                        MailItemList.Add(myitem);
                    }

                    totalMailItems = MailItemList.Count;
                    Console.WriteLine("\t\tMail Items found: {0}", totalMailItems.ToString());



                    //Saves List of all Report Items
                    foreach (ReportItem myitem in workingFolder.Items.OfType <ReportItem>())
                    {
                        ReportItemList.Add(myitem);
                    }

                    totalReportItems = ReportItemList.Count;
                    Console.WriteLine("\t\tReport Items found: {0}", totalReportItems.ToString());

                    //Pause
                    //Console.ReadKey();

                    //Processing of
                    //Invalid Email
                    //Address


                    new ExtractData().GetMailItemsData(MailItemList, processedMailFolder, exceptionMailFolder);

                    new ExtractData().GetReportItemsData(ReportItemList, processedReportFolder, exceptionReportFolder);
                }
                else
                {
                    Console.WriteLine("\nNo Outlook item found for processing.");
                }
                #endregion doingStuff
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.WriteLine("\nLogging Off...\n");
                outlookNameSpace.Logoff();
                outlookNameSpace   = null;
                outlookApplication = null;
                inboxFolder        = null;
                Console.WriteLine("Process Ended. Press any key to close this Black Window...");
                //Console.ReadKey();
            }
        }
        public static MailItem GetEmail(string subject, string recipientEmail, DateTime maxAgeTimeStamp, int waitSeconds)
        {
            const int    checkIntervalSeconds = 15;
            const string prSmtpAddress        = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

            if (Process.GetProcessesByName("outlook").Length == 0)
            {
                Process.Start("outlook.exe");
            }

            var item = new MailItem();

            NetOffice.OutlookApi.MailItem oItem = null;
            var match = false;

            try
            {
                Application app = new Application();
                _NameSpace  ns  = app.GetNamespace("MAPI");
                ns.Logon(null, null, false, false);

                MAPIFolder inboxFolder = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                _Items     oItems      = inboxFolder.Items;
                oItems.Sort("[ReceivedTime]", false);
                Console.WriteLine("DBG: Started looking for email at {0}", DateTime.Now);
                for (int j = 0; j <= waitSeconds; j = j + checkIntervalSeconds)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(checkIntervalSeconds));
                    for (int i = oItems.Count; i > 1; i--)
                    {
                        oItem = (NetOffice.OutlookApi.MailItem)oItems[i];
                        Console.WriteLine("DBG: Checking mail at {0} => found mail with timestamp: { 1}", DateTime.Now.ToLongTimeString(), oItem.SentOn.ToLongTimeString());
                        if ((oItem.ReceivedTime - maxAgeTimeStamp).TotalSeconds < 0)
                        {
                            break;
                        }
                        if (oItem.Subject.IsRegExMatch(subject) && oItem.Recipients.Single().PropertyAccessor.GetProperty(prSmtpAddress).ToString().Equals(recipientEmail))
                        {
                            match = true;
                            break;
                        }
                        //Console.WriteLine("Subject: {0}", item.Subject);
                        //Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
                    }
                    if (match)
                    {
                        break;
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                throw new Exception("ERROR: retrieving the email failed due to System.Runtime.InteropServices.COMException");
            }

            if (match)
            {
                item.EntryId      = oItem.EntryID;
                item.Subject      = oItem.Subject;
                item.SentDate     = oItem.SentOn.ToLongDateString();
                item.SentTime     = oItem.SentOn.ToLongTimeString();
                item.ReceivedDate = oItem.ReceivedTime.ToLongDateString();
                item.ReceivedTime = oItem.ReceivedTime.ToLongTimeString();
                item.Body         = oItem.Body;
                item.HtmlBody     = oItem.HTMLBody;
                item.HasMessage   = true;
                item.PrintInfo();
            }
            else
            {
                Console.WriteLine("DBG: Couldn't find message with subject matching {0} and recipient {1}", subject, recipientEmail);
            }
            Console.WriteLine("DBG: Finished looking for email at {0}", DateTime.Now);
            return(item);
        }