Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

Checking outlook email from a C# Windows Form

Checking email from a Windows form in C#

Good morning experts,

I am trying to find a way to check email coming through our Outlook exchange (2007) . I came across the  code listed below on the internet.
The code works up to a point. It reaches the inbox folder and display one of the emails in it.
The problems is it does this at random. It doesn’t retrieve the last email or even today’s email. What is more, when the form is reloaded and the code is run again, the same email is being displayed.
Please advise. Thank you


        private void checkIncomingMail()
        {
            Outlook.Application OutlookApp = new Outlook.Application();
           
            this.txtBody.Visible = true; // email text
            this.txtTicketBody.Visible = false;
            this.lblSubject.Text = "Send To";

            Outlook._Application myApp = new Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            if (myInbox.Items.Count > 0)
            {
                this.txtSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
                lblSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
                //Grab the Attachment Name
                if (((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments.Count > 0)
                {
                    lblAttachmentName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments[1].FileName;
                }
                else
                {
                    lblAttachmentName.Text = "No Attachment";
                }
                // Grab the Body
                txtBody.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;
                // Sender Name
                lblSenderName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderName;
                // Sender Email
                lblSenderEmail.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;
                // Creation date
                lblCreationdate.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).CreationTime.ToString();

            }
            else
            {
                MessageBox.Show("There are no emails in your Inbox.");
            }
        }
Avatar of Naitik Gamit
Naitik Gamit
Flag of India image

Look at this project of access outlook email from a C# Windows Form:

http://www.programminghelp.com/dotnet/access-your-email-within-outlook-final-part-c/
You are accessing Items[1]. You need to loop through the Items collection.
Avatar of adamtrask
adamtrask

ASKER

Naitik Gamit, Thank you for the link. I believe this is the same code I was referring to.
 It does work, but only shows one email without any option to show others. It displays the same email each time I click on the "Access Email" button. There is no guide on how we can develop this further.
You need to tell us what you exactly want the code to do. You only told us it shows one email (only logical), and that you want to "develop this furter". But that doesn't give us ANY information at all what you really want it to do, except maybe get only the newest email.
Sorry for not being clear. I want a way to show each email that has not been read.
You need to loop through the Items collection.
Sorry Éric Moreau, I didn't see your earlier post.... will try that
How do you want it to show. Your form only "supports" one message. Do you want to keep clicking on a NEXT button, or do you want to change the form to a list or something.
Yes, clicking on a next button would be a good solution. Thanks.
Ok,

With further research I found the code listed below. It works fine as far as displaying each email. The only problem is that it displays the emails in a messageBox which display one email then pause until the user clicks OK then moves on to display the next email.
I need to display each message in a designated textBox for messages. I need to make the click event move from one email to the next.
Ok,

With further research I found the code listed below. It works fine as far as displaying each email. The only problem is that it displays the emails in a message Box which display one email then pause until the user clicks OK then moves on to display the next email.
I need to display each message in a designated text Box for messages. I need to make the click event move from one email to the next.

 private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.ApplicationClass();

            Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI");

            Microsoft.Office.Interop.Outlook.MAPIFolder objFolder = NS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

           Microsoft.Office.Interop.Outlook.MailItem objMail;
           
            Microsoft.Office.Interop.Outlook.Items oItems;

            oItems = objFolder.Items;

            objMail = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetFirst();

            for (int i = 1; i <= oItems.Count; i++)
            {
                objMail = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetNext();
             
              MessageBox.Show(objMail.Body.ToString());
             
             
             
            }

        }
you will need to replace:
MessageBox.Show(objMail.Body.ToString());

Open in new window


with:
YourDesignatedTB.Text = objMail.Body.ToString();

Open in new window

I did that. The problem is the textBox would display the emails, one after the other without a pause. The messageBox pauses the flow of emails until you click OK.
But not the textBox!
I spent a good time trying to find a way around this without success.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Éric Moreau....