Connecting Tech Pros Worldwide Forums | Help | Site Map

Help Looping Through my Inbox and extracting attachments

Steve Le Monnier
Guest
 
Posts: n/a
#1: Aug 2 '06
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



pigeonrandle
Guest
 
Posts: n/a
#2: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


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:
Quote:
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
Steve Le Monnier
Guest
 
Posts: n/a
#3: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


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@hotmail.comwrote in message
news:1154516548.297051.253000@75g2000cwc.googlegro ups.com...
Quote:
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:
Quote:
>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
>

pigeonrandle
Guest
 
Posts: n/a
#4: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


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:
Quote:
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@hotmail.comwrote in message
news:1154516548.297051.253000@75g2000cwc.googlegro ups.com...
Quote:
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:
Quote:
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
Steve Le Monnier
Guest
 
Posts: n/a
#5: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


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@hotmail.comwrote in message
news:1154518141.588218.64830@b28g2000cwb.googlegro ups.com...
Quote:
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:
Quote:
>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@hotmail.comwrote in message
>news:1154516548.297051.253000@75g2000cwc.googlegr oups.com...
Quote:
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
>
>

pigeonrandle
Guest
 
Posts: n/a
#6: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


Steve,
Just found this on MS - any good?

http://msdn.microsoft.com/library/de...rp05152003.asp

James.

Steve Le Monnier wrote:
Quote:
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@hotmail.comwrote in message
news:1154518141.588218.64830@b28g2000cwb.googlegro ups.com...
Quote:
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:
Quote:
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@hotmail.comwrote in message
news:1154516548.297051.253000@75g2000cwc.googlegro ups.com...
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
Steve Le Monnier
Guest
 
Posts: n/a
#7: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


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@hotmail.comwrote in message
news:1154520099.210777.326240@p79g2000cwp.googlegr oups.com...
Quote:
Steve,
Just found this on MS - any good?
>
http://msdn.microsoft.com/library/de...rp05152003.asp
>
James.
>
Steve Le Monnier wrote:
Quote:
>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@hotmail.comwrote in message
>news:1154518141.588218.64830@b28g2000cwb.googlegr oups.com...
Quote:
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@hotmail.comwrote in message
>news:1154516548.297051.253000@75g2000cwc.googlegr oups.com...
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
>
>
>

pigeonrandle
Guest
 
Posts: n/a
#8: Aug 2 '06

re: Help Looping Through my Inbox and extracting attachments


No worries. Glad i could help.

James

Steve Le Monnier wrote:
Quote:
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@hotmail.comwrote in message
news:1154520099.210777.326240@p79g2000cwp.googlegr oups.com...
Quote:
Steve,
Just found this on MS - any good?

http://msdn.microsoft.com/library/de...rp05152003.asp

James.

Steve Le Monnier wrote:
Quote:
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@hotmail.comwrote in message
news:1154518141.588218.64830@b28g2000cwb.googlegro ups.com...
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@hotmail.comwrote in message
news:1154516548.297051.253000@75g2000cwc.googlegro ups.com...
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
Jonathan Roberts
Guest
 
Posts: n/a
#9: Aug 3 '06

re: Help Looping Through my Inbox and extracting attachments



"Steve Le Monnier" <steve_lemon@no.spam.hotmail.comwrote in message
news:%23iy1W9itGHA.2448@TK2MSFTNGP06.phx.gbl...
Quote:
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.
You might want to look into JMail from www.dimac.net.



Closed Thread