Get mails from the Inbox?

Add-in Express™ Support Service
That's what is more important than anything else

Get mails from the Inbox?
Export mails from the Outlook Inbox 
Peppi


Guest


Hello everybody,

I try to export mails from my Outlook inbox with C#.
I try it with this statement:
for (int i = 1; i <= outlookApp.ActiveExplorer().CurrentFolder.Items.Count; i++)
{
outlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items.Item(i)
}

But all what I get are this reference: System.__ComObject. How can I get the parts of the mail (e.g. subject, body and especially an flag if the mail is new).

Regrads!

peppi
Posted 08 Dec, 2004 08:50:36 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Peppi,

Try the following code to get mail items in Outlook.
In order to check whether a mail item is a new mail you can use the Sent property of Outlook.MailItem interface. If it returns false it means this is a new mail.

private void adxCommandBarButton1_Click(object sender)
{
Outlook.Items items = null;
Outlook.MAPIFolder folder = null;
Outlook.MailItem mail = null;
Outlook.NameSpace namespace_ = null;
string body = String.Empty;
string subject = String.Empty;

try
{
namespace_ = OutlookApp.GetNamespace("MAPI");
if (namespace_ != null)
{
folder = namespace_.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
if (folder != null)
{
try
{
items = folder.Items;
for(int i=1; i<=items.Count; i++)
{
mail = items.Item(i) as Outlook.MailItem;
if (mail != null)
{
subject = mail.Subject;
body = mail.Body;
Marshal.ReleaseComObject(mail); mail = null;
}
}
}
catch(Exception e)
{
if (mail != null)
{
MessageBox.Show(e.Message, e.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
Marshal.ReleaseComObject(mail); mail = null;
}
}
}
}
}
finally
{
if (items != null)
Marshal.ReleaseComObject(items);
if (folder != null)
Marshal.ReleaseComObject(folder);
if (namespace_ != null)
Marshal.ReleaseComObject(namespace_);
}
}
Posted 08 Dec, 2004 09:27:14 Top
Peppi


Guest


Hi everybody,

@ Sergey: Now I understand how it works. But I've got one problem. Which Reference I have to add for the Marshal-class?

Regrads!

Peppi
Posted 08 Dec, 2004 09:59:42 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
You should use Marshal class with System.__ComObject references.
See MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassreleasecomobjecttopic.asp
You can see in the debug mode what happens with System.__ComObject reference after the Mashal.ReleaseComObject method was applied to it.
Posted 08 Dec, 2004 10:14:16 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Peppi,
I mean that you should use Marshal class with references that store COM interfaces, e.g Outlook interfaces.
Posted 08 Dec, 2004 10:24:14 Top
Patrick Behr




Posts: 12
Joined: 2004-12-09
Hi everybody,

@ Sergey: Now I understand the sense of the marshal-class. Thank you!

Is there the possibility to find out which mails in my Outlook inbox are marked (tagged). I only want to get the subject, body et cetera of these mails.

Regrads!
Posted 09 Dec, 2004 03:31:45 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Patrick,

Outlook._MailItem interface has the FlagStatus property. This property can return the following values:
olFlagComplete
olFlagMarked
olNoFlag
I hope it will help you.
Posted 09 Dec, 2004 07:15:55 Top
Patrick Behr




Posts: 12
Joined: 2004-12-09
Hi Sergey,

I try to use this property, bu it don't run.
for(int i=1; i<=items.Count; i++)
{
mailItem = items.Item(i) as Outlook.MailItem;
MessageBox.Show(mailItem.FlagStatus);
}

But if I marked a mail in the Outlook Inbox (by clicking it) and I run through all MailItems (with my Programm) the value of the FlagStatus is allways olNoFlag. ???

Posted 09 Dec, 2004 07:34:19 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hmmm... it is very strange, because I use the following code and I get flags correctly.

private void adxCommandBarButton2_Click(object sender)
{
object selectedItem = null;
Outlook.Selection selection = null;
Outlook._Explorer activeExplorer = OutlookApp.ActiveExplorer();
Outlook.MailItem mail = null;
try
{
if (activeExplorer != null)
{
try
{
selection = activeExplorer.Selection;
if (selection.Count > 0)
{
selectedItem = selection.Item(1);
if (selectedItem is Outlook.MailItem)
{
mail = selectedItem as Outlook.MailItem;
MessageBox.Show(mail.FlagStatus.ToString());
}
}
}
catch
{
}
}
}
finally
{
if (selectedItem != null)
Marshal.ReleaseComObject(selectedItem);
if (selection != null)
Marshal.ReleaseComObject(selection);
if (activeExplorer != null)
Marshal.ReleaseComObject(activeExplorer);
}
}

What version of Outlook do you use?


Posted 09 Dec, 2004 07:56:39 Top
Patrick Behr




Posts: 12
Joined: 2004-12-09
I use Outlook XP (Link Library 10.0)
Posted 09 Dec, 2004 09:13:20 Top