예제 #1
0
        static void Main(string[] args)
        {
            var mails = OutlookEmails.ReadMailItems();
            //TEMP Iterator for test
            int i = 1;

            foreach (var mail in mails)
            {
                //TEMP :: printing to console as test
                Console.WriteLine("Mail No " + i);
                Console.WriteLine("Mail Recieved From: " + mail.EmailFrom);
                Console.WriteLine("Mail Subject: " + mail.EmailSubject);
                Console.WriteLine("");
                i = i + 1;
                //TEMP
            }
            Console.ReadKey();
        }
예제 #2
0
        //function that compiles the needed info
        public static List <OutlookEmails> ReadMailItems()
        {
            Application          outlookApplication = null;
            NameSpace            outlookNamespace   = null;
            MAPIFolder           inboxFolder        = null;
            Items                mailItems          = null;
            List <OutlookEmails> listEmailDetails   = new List <OutlookEmails>();
            OutlookEmails        emailDetails;

            //try block to add from subject and body into a new item
            try
            {
                outlookApplication = new Application();
                outlookNamespace   = outlookApplication.GetNamespace("MAPI");
                //Select email inbox to read
                inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                mailItems   = inboxFolder.Items;
                foreach (MailItem item in mailItems)
                {
                    emailDetails              = new OutlookEmails();
                    emailDetails.EmailFrom    = item.SenderEmailAddress;
                    emailDetails.EmailSubject = item.Subject;
                    emailDetails.EmailBody    = item.Body;
                    listEmailDetails.Add(emailDetails);
                    ReleaseComObject(item);
                }
            }
            catch (System.Exception ex)

            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                //releasing objects used by program
                ReleaseComObject(mailItems);
                ReleaseComObject(inboxFolder);
                ReleaseComObject(outlookNamespace);
                ReleaseComObject(outlookApplication);
            }
            return(listEmailDetails);
        }