Why can't I retrieve ALL MailItems using interop outlook? - c#

I'm trying to use Microsoft.Office.Interop.Outlook to retrieve emails from my Outlook inbox. This is my code:
Application app = new Application();
NameSpace ns = app.Session;
MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Items items = inbox.Items;
foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
{
if (mail as MailItem != null)
{
Console.WriteLine(mail.Subject.ToString());
Console.WriteLine(mail.Body.ToString());
Console.ReadKey();
}
}
When I do this, it works--sort of. It only shows one email. There should be three. The email it's showing is the oldest one in there... why wouldn't I be able to get all three? Is there some other type of mail besides MailItem that would be in my inbox?

I had this same exact problem - My workaround was just to create a List<MailItem> and loop through that. Make sure the emails aren't in subfolders though, otherwise they won't be found.
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
List<MailItem> ReceivedEmail = new List<MailItem>();
foreach (Outlook.MailItem mail in emailFolder.Items)
ReceivedEmail.Add(mail);
foreach (MailItem mail in ReceivedEmail)
{
//do stuff
}

Related

Get Outlook AppointmentItem using GlobalAppointmentID

I am developing a calendar for my firm that synchronizes with Outlook Calendar.
Atm I can:
import appointments from Outlook and show them in my calendar
update my appointments when Outlook appointments get updated
create Outlook appointments when appointments get created in my calendar
The only issue I have is updating/deleting Outlook appointments when my appointments update/delete.
I have the GlobalAppointmentID of the corresponding appointments but I can't seem to search on that ID.
I tried:
using Microsoft.Office.Interop;
private void GetAppointment(string myGlobalAppointmentID)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace mapiNamespace = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)outlookCalendarItems.Find("[GlobalAppointmentID] = '{0}'", myGlobalAppointmentID));
//update or delete appointmentItem here (which I know how to do)
}
I keep getting 'Condition is not valid' Exception.
Apparently Outlook does not allow to search on binary properties (such as GlobalAppointmentID).
I use the same outlookCalendarItems.Find() and calendarFolder.Items.Restrict() without problems in other instances.
I tried using Redemption but I couldn't get it to work either.
Does anybody have experience or a suggestion?
Yes, OOM won't let you search on binary properties (as well as recipients or attachments), but Redemption (I am its author) should work. The following script (VBA) worked just fine for me:
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetDefaultFolder(olFolderCalendar)
set appt = Folder.Items.Find("GlobalAppointmentID = '040000008200E00074C5B7101A82E00800000000D0FECEE58FEAD70100000000000000001000000041C887A3FA12694F8A0402FEFFAD0BBB'")
MsgBox appt.Subject
The Outlook object model doesn't support searching for binary properties such as GlobalAppointmentId (any other PT_BINARY property) with Items.Find/Items.FindNext/Items.Restrict.
The only workaround is to loop through all item in the Calendar folder (which is inefficient) or search using Extended MAPI (or using third-party wrappers such as Redemption.
What I ended up doing after looking further:
I added a text UserProperty where I put the GlobalAppointmentID("GAID") in.
You can Filter on those.
And it seems to do the trick.
private void AddGAIDIfNeeded(Outlook.AppointmentItem app)
{
bool GAIDexists = false;
if (app.UserProperties.Count != 0)
{
foreach (UserProperty item in app.UserProperties)
{
if (item.Name == "GAID")
{
GAIDexists = true;
break;
}
}
}
if (GAIDexists == false)
{
app.UserProperties.Add("GAID", Outlook.OlUserPropertyType.olText);
app.UserProperties["GAID"].Value = app.GlobalAppointmentID;
app.Save();
}
}
And to find a specific AppointmentItem:
private void DeleteOutlookAppointmentByGAID(string globalAppointmentID)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace mapiNamespace = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
try
{
Outlook.AppointmentItem appointmentItem = null;
appointmentItem = outlookCalendarItems.Find(String.Format("[GAID] = '{0}'", globalAppointmentID));
if (appointmentItem != null)
{
appointmentItem.Delete();
}
}
catch (Exception ex)
{
classExecptionLogging.LogErrorToFolder(ex);
}
}

What's wrong with my query to send an email via Outlook with C#

As an image below is my problem with the Application. I've tried to add
Outlook.Application Application = new Outlook.Application();
but nothing changes
I added namespace as using Outlook = Microsoft.Office.Interop.Outlook;
#region Send Email
private void SendEmail()
{
string subjectEmail = "Meeting has been rescheduled.";
string bodyEmail = "Meeting is one hour later.";
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
foreach (Outlook.ContactItem contact in sentContacts.Items)
{
if (contact.Email1Address.Contains("example.com"))
{
this.CreateEmailItem(subjectEmail, contact
.Email1Address, bodyEmail);
}
}
}
private void CreateEmailItem(string subjectEmail, string toEmail, string bodyEmail)
{
Outlook.Application Application = new Outlook.Application();
Outlook.MailItem eMail = (Outlook.MailItem)
this.Application.CreateItem(Outlook.OlItemType.olMailItem);
eMail.Subject = subjectEmail;
eMail.To = toEmail;
eMail.Body = bodyEmail;
eMail.Importance = Outlook.OlImportance.olImportanceLow;
((Outlook._MailItem)eMail).Send();
}
#endregion
This line of code is looking for a class-level field called Application, which I assume doesn't exist:
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
Create an instance of Application inside the method (like you said you did) and then remove the keyword this from the above code:
Outlook.Application Application = new Outlook.Application();
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
If you need access to Application outside of that method, then create Application at the class level (outside of any methods) and instantiate it inside the constructor (if that's appropriate in your situation).

Not setting the property of incoming mails in MS Outlook 2010

I am setting the property (for making them As Read and with High Importance) of the mail those are coming to the MS Outlook 2010 inbox using below code -
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.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);
int i = myInbox.Items.Count;
((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).UnRead = false;
((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).Importance = OlImportance.olImportanceHigh;
This works fine when only one mail comes at a time (I can see the mail as Read and with High Importance) after the code execution but when three or four mails coming at a time then it set the property of only one mail not for all the three or four mails.
Please suggest.
Remember to save the message after setting any property.
Most importantly, your code uses multiple dot notation - for each ".", you get back a brand new COM object, so you end up setting Importance property on an object different from the one used to set the UnRead property.
int i = myInbox.Items.Count;
MailItem msg = (Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i];
msg.UnRead = false;
msg.Importance = OlImportance.
msg.Save();
Another problem is that you assume that the last item in the Items collection is the latest one. This is not generally true. As cremor suggested, use Items.ItemAdd event, but still do not forget to save the message.
You can use the ItemAdd event of the Items property of the folder:
Items inboxItems = myInbox.Items;
inboxItems.ItemAdd += HandleItemAdded;
private void HandleItemAdded(object item)
{
MailItem mail = item as MailItem;
if (mail == null) { return; }
mail.UnRead = false;
mail.Importance = OlImportance.olImportanceHigh;
}

Retrieve Current Email Body In Outlook

in my outlook addin I want to add a button on the Ribbon so when user click this button I want to retrieve the current selected email's body , I have this code but it retrieve only the first email from the inbox because the index is 1 :
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.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);
String body = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;
so how to retrieve the current opened email in outlook? , this method work for me but I need to get the index for the current email.
Thanks.
You shouldn’t initialize a new Outlook.Application() instance each time. Most add-in frameworks provide you with an Outlook.Application instance, corresponding to the current Outlook session, typically through a field or property named Application. You are expected to use this for the lifetime of your add-in.
To get the currently-selected item, use:
Outlook.Explorer explorer = this.Application.ActiveExplorer();
Outlook.Selection selection = explorer.Selection;
if (selection.Count > 0) // Check that selection is not empty.
{
object selectedItem = selection[1]; // Index is one-based.
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
if (mailItem != null) // Check that selected item is a message.
{
// Process mail item here.
}
}
Note that the above will let you process the first selected item. If you have multiple items selected, you might want to process them in a loop.
On Top add reference to
using Outlook = Microsoft.Office.Interop.Outlook;
Then inside a method;
Outlook._Application oApp = new Outlook.Application();
if (oApp.ActiveExplorer().Selection.Count > 0)
{
Object selObject = oApp.ActiveExplorer().Selection[1];
if (selObject is Outlook.MailItem)
{
Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
String htmlBody = mailItem.HTMLBody;
String Body = mailItem.Body;
}
}
Also you can change the body which will show in outlook before viewing the mail.

How to get unread mail from the specific folder

I am using below code to check unread mail from the outlook
and everything is working fine for the default inbox folder
Microsoft.Office.Interop.Outlook.Application oApp;
Microsoft.Office.Interop.Outlook._NameSpace oNS;
Microsoft.Office.Interop.Outlook.MAPIFolder oFolder;
Microsoft.Office.Interop.Outlook._Explorer oExp;
oApp = new Microsoft.Office.Interop.Outlook.Application();
oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;
foreach (Object mail in items)
{
if ((mail as Microsoft.Office.Interop.Outlook.MailItem) != null && (mail as Microsoft.Office.Interop.Outlook.MailItem).UnRead == true)
{
string sasd= (mail as OutLook.MailItem).Subject.ToString();
}
}
But I want to check another folder [which I have created [Name = "Inbox_Personal"]]. How can I do that?
Edit 1
Any suggestion or reference to the tutorial will be appreciated.
I use something similar to the following to access different accounts in Outlook (2007 and greater; before 2007 stores do not exist and you simply need to look at the folders)
Microsoft.Office.Interop.Outlook.Application oApp;
Microsoft.Office.Interop.Outlook.NameSapce oNS = oApp.GetNameSpace(“Mapi”);
foreach(Microsoft.Office.Interop.Outlook.Store oAccounts in oNS.Stores)
{
// get the right account:
Microsoft.Office.Interop.Outlook.Store oDesiredAccount;
foreach(Microsoft.Office.Interop.Outlook.Store oAccount in oAccounts)
{
if(oAccount.DisplayName.ToLower.Equals(“<<Name of Account>>”)
{
oDesiredAccount = oAccount;
}
}
// do stuff with the account
Microsoft.Office.Interop.Outlook.MAPIFolder root = oAccount.GetRootFolder();
// ....
}
var fld = (Outlook.Folder)app.Session.GetFolderFromID("Inbox_Personal", storeID);
I can't remember where to get the store ID from, but should be stored in your session object oder default folder object.
EDIT
I've looked up in a project now: StoreID in GetFolderFromID is optional (Type.Missing).
Default Store ID can be found here:
app.Session.DefaultStore.StoreID
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._namespace.defaultstore(v=office.12).aspx

Categories

Resources