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.O utlook.Nam eSpace mapiNameSpace = myApp.GetNamespace("MAPI") ;
Microsoft.Office.Interop.O utlook.MAP IFolder myInbox = mapiNameSpace.GetDefaultFo lder(Micro soft.Offic e.Interop. Outlook.Ol DefaultFol ders.olFol derInbox);
if (myInbox.Items.Count > 0)
{
this.txtSubject.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Subj ect;
lblSubject.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Subj ect;
//Grab the Attachment Name
if (((Microsoft.Office.Intero p.Outlook. MailItem)m yInbox.Ite ms[1]).Att achments.C ount > 0)
{
lblAttachmentName.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Atta chments[1] .FileName;
}
else
{
lblAttachmentName.Text = "No Attachment";
}
// Grab the Body
txtBody.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Body ;
// Sender Name
lblSenderName.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Send erName;
// Sender Email
lblSenderEmail.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Send erEmailAdd ress;
// Creation date
lblCreationdate.Text = ((Microsoft.Office.Interop .Outlook.M ailItem)my Inbox.Item s[1]).Crea tionTime.T oString();
}
else
{
MessageBox.Show("There are no emails in your Inbox.");
}
}
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
this.lblSubject.Text = "Send To";
Outlook._Application myApp = new Outlook.Application();
Microsoft.Office.Interop.O
Microsoft.Office.Interop.O
if (myInbox.Items.Count > 0)
{
this.txtSubject.Text = ((Microsoft.Office.Interop
lblSubject.Text = ((Microsoft.Office.Interop
//Grab the Attachment Name
if (((Microsoft.Office.Intero
{
lblAttachmentName.Text = ((Microsoft.Office.Interop
}
else
{
lblAttachmentName.Text = "No Attachment";
}
// Grab the Body
txtBody.Text = ((Microsoft.Office.Interop
// Sender Name
lblSenderName.Text = ((Microsoft.Office.Interop
// Sender Email
lblSenderEmail.Text = ((Microsoft.Office.Interop
// Creation date
lblCreationdate.Text = ((Microsoft.Office.Interop
}
else
{
MessageBox.Show("There are no emails in your Inbox.");
}
}
You are accessing Items[1]. You need to loop through the Items collection.
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.
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.
ASKER
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.
ASKER
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.
ASKER
Yes, clicking on a next button would be a good solution. Thanks.
ASKER
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.
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.
ASKER
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.O utlook.App lication app = new Microsoft.Office.Interop.O utlook.App licationCl ass();
Microsoft.Office.Interop.O utlook.Nam eSpace NS = app.GetNamespace("MAPI");
Microsoft.Office.Interop.O utlook.MAP IFolder objFolder = NS.GetDefaultFolder(Micros oft.Office .Interop.O utlook.OlD efaultFold ers.olFold erInbox);
Microsoft.Office.Interop.O utlook.Mai lItem objMail;
Microsoft.Office.Interop.O utlook.Ite ms oItems;
oItems = objFolder.Items;
objMail = (Microsoft.Office.Interop. Outlook.Ma ilItem)oIt ems.GetFir st();
for (int i = 1; i <= oItems.Count; i++)
{
objMail = (Microsoft.Office.Interop. Outlook.Ma ilItem)oIt ems.GetNex t();
MessageBox.Show(objMail.Bo dy.ToStrin g());
}
}
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.O
Microsoft.Office.Interop.O
Microsoft.Office.Interop.O
Microsoft.Office.Interop.O
Microsoft.Office.Interop.O
oItems = objFolder.Items;
objMail = (Microsoft.Office.Interop.
for (int i = 1; i <= oItems.Count; i++)
{
objMail = (Microsoft.Office.Interop.
MessageBox.Show(objMail.Bo
}
}
you will need to replace:
with:
MessageBox.Show(objMail.Body.ToString());
with:
YourDesignatedTB.Text = objMail.Body.ToString();
ASKER
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.
But not the textBox!
I spent a good time trying to find a way around this without success.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Éric Moreau....
http://www.programminghelp.com/dotnet/access-your-email-within-outlook-final-part-c/