Re: Help Looping Through my Inbox and extracting attachments

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Cheers James

I've read the article and download the sample code... and eureka it does
what I need. It seems the author has written his own wrapper around mapi to
provide for the missing enumrator.

It's very clever and more importantly it works... but boy what a pain
considering it could be done with 6 lines of VB code. MS need to make some
improvments here.

I'm going to play with this further, but might consider using VBScript to
get the attachments out before switching back to C# to process them.

Thanks for all your help today

Steve


"pigeonrandle" <pigeonrandle@xxxxxxxxxxx> wrote in message
news:1154520099.210777.326240@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Steve,
Just found this on MS - any good?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05152003.asp

James.

Steve Le Monnier wrote:
If only it was that easy.

I'm surprised how little sample code their is on this topic... but given
how
difficult it is to do anything may be that's why. If I can't crack this
soon
then it's back to VBScript :-(

Thanks anyway.


"pigeonrandle" <pigeonrandle@xxxxxxxxxxx> wrote in message
news:1154518141.588218.64830@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Steve,
Don't you just love it! Have you tried,

for (int i =0; i< oMsgs.Count; i++)
{
MAPI.Message oMsg = oMsgs[i];
//do your thing here
}

oMsgs.Count might be oMsgs.Length ...

Cheers,
James

Steve Le Monnier wrote:
Yeah I've tried that, I get an error message stating:

Error 1 foreach statement cannot operate on variables of type
'MAPI.Messages' because 'MAPI.Messages' does not contain a public
definition
for 'GetEnumerator'

VB6/.NET doesn't care and lets you take shortcuts but C# requires
everything
to be strongly typed. It's a real bind as what I want to do can be
done
with
6 lines of VB but I'm trying to keep everything in C#.

Cheers

Steve


"pigeonrandle" <pigeonrandle@xxxxxxxxxxx> wrote in message
news:1154516548.297051.253000@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Steve,
Have you tried

MAPI.Session oSession = new MAPI.Session();
MAPI.Folder oFolder;
oSession.Logon("Outlook", vEmpty, false, true, 0, true, vEmpty);
oFolder = (MAPI.Folder)oSession.Inbox;
MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;

foreach (MAPI.Message oMsg in oMsgs)
{
//use oMsg variable here
}

I dont know what type 'MAPI.Message' should really be (i may be
right),
but you will be able to find out from looking at the 'MAPI.Messages'
collection type.

HTH,
James

Steve Le Monnier wrote:
Can anybody help with the following: I have some old VB6 code that
loops
through my inbox looking for emails with attachments and then save
them
to
the hard drive. The code is very simplistic so I thought it would
be
dead
easy to migrate it to C#, however it is proving more difficult than
I
first
thought.

The VB code is as follows:
Set oSession = CreateObject("MAPI.Session")
oSession.Logon("Outlook", , False)

Set oInbox = oSession.Inbox

For Each oMsg In oInbox.Message
oMsg.Attachments(1).WriteToFile {path}
Next

My C# alternative so far is:
MAPI.Session oSession = new MAPI.Session();
MAPI.Folder oFolder;
oSession.Logon("Outlook", vEmpty, false, true, 0, true,
vEmpty);
oFolder = (MAPI.Folder)oSession.Inbox;
MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;

The problem I am having is not being able to loop through the
messages
so
I
can check who sent it and does it have an attachment?

Is it possible to translate the VB6 code to C#?

Many thanks

Steve Le Monnier





.



Relevant Pages