Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Multiple Outlook Email for multiple IDs in C#

Referring to the answer mentioned in the post: Reading Outlook Mail with C#

This code works well for a single account, but the issue with the current code is that it reads the email of the default id which was set as the first one in the outlook app.

For example, if "[email protected]" is set as the first account and "[email protected]" is set as the second one, then it reads the inbox of only the first id, i.e., "[email protected]". I tried doing some modifications in the code but it did not work. Below is my code:

  public static void OutLookMailStart(string EmailID, string password)
    {
        Microsoft.Office.Interop.Outlook.Application app = null;
        Microsoft.Office.Interop.Outlook._NameSpace ns = null;
        Microsoft.Office.Interop.Outlook.PostItem item = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
        Microsoft.Office.Interop.Outlook.MailItem mailItem = null;
        try
        {
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("MAPI");

            ns.Logon(EmailID, password, false, true);
            inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

            foreach (Object mail in inboxFolder.Items)
            {
                if ((mail as MailItem) != null && (mail as MailItem).UnRead == true)
                {
                    // Email Subject 
                    string Subject = (mail as MailItem).Subject.ToString();
                    if (Subject.Contains(FileDomain))
                    {
                        // Email Body
                        var ReplyText = ExtractReply(((mail as MailItem).Body.ToString()), FromTrimName);
                    }
                    // (mail as MailItem).UnRead = false;
                    (mail as MailItem).Save();
                }
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            ns.Logoff();
            inboxFolder = null;
            subFolder = null;
            mailItem = null;
            app = null;
        }}

Any sort of help will be appreciated.

like image 894
Yogesh Patel Avatar asked Nov 15 '25 01:11

Yogesh Patel


1 Answers

 public static void OutLookMailStart(string EmailID, string password)
        {
            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace ns = null;
            Microsoft.Office.Interop.Outlook.PostItem item = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
            Microsoft.Office.Interop.Outlook.MailItem mailItem = null;


            try
            {
                app = new Microsoft.Office.Interop.Outlook.Application();
                ns = app.GetNamespace("MAPI");

                ns.Logon(EmailID, password, true, true);

                inboxFolder = ns.Folders[EmailID].Folders[2];

                foreach (Microsoft.Office.Interop.Outlook.MailItem mailItemm in inboxFolder.Items)
                {
                    if (mailItemm.UnRead) // I only process the mail if unread
                    {
                        // Email Subject 
                        Console.WriteLine("Subject : {0}", mailItemm.Subject);
                        string Subject = mailItemm.Subject;
                        if (!string.IsNullOrEmpty(Subject) && !string.IsNullOrWhiteSpace(Subject))
                        {
                            if (Subject.Contains(FileDomain))
                            {


                                var SenderName = mailItemm.Sender.Name;
                                //Email Body
                                Console.WriteLine("Accounts: {0}", mailItemm.Body);




                                // Read All Attachements  
                                var attachments = (mailItemm as MailItem).Attachments;
                                if (attachments != null && attachments.Count > 0)
                                {
                                    for (int i = 1; i <= attachments.Count; i++)
                                    {
                                        attachments[i].SaveAsFile(tempFolderPath + (mailItemm as MailItem).Attachments[i].FileName);


                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                ns.Logoff();
                inboxFolder = null;
                subFolder = null;
                mailItem = null;
                app = null;
            }
        }
like image 128
krishna parab Avatar answered Nov 17 '25 20:11

krishna parab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!