Help Looping Through my Inbox and extracting attachments

  • Thread starter Steve Le Monnier
  • Start date
S

Steve Le Monnier

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
 
P

pigeonrandle

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
 
S

Steve Le Monnier

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
 
P

pigeonrandle

Steve,
Don't you just love it! Have you tried,

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

oMsgs.Count might be oMsgs.Length ...

Cheers,
James
 
S

Steve Le Monnier

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 said:
Steve,
Don't you just love it! Have you tried,

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

oMsgs.Count might be oMsgs.Length ...

Cheers,
James
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
 
P

pigeonrandle

Steve,
Just found this on MS - any good?

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

James.
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 said:
Steve,
Don't you just love it! Have you tried,

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

oMsgs.Count might be oMsgs.Length ...

Cheers,
James
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


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
 
S

Steve Le Monnier

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 said:
Steve,
Just found this on MS - any good?

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

James.
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 said:
Steve,
Don't you just love it! Have you tried,

for (int i =0; i< oMsgs.Count; i++)
{
MAPI.Message oMsg = oMsgs;
//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


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

 
P

pigeonrandle

No worries. Glad i could help.

James
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 said:
Steve,
Just found this on MS - any good?

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

James.
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.


Steve,
Don't you just love it! Have you tried,

for (int i =0; i< oMsgs.Count; i++)
{
MAPI.Message oMsg = oMsgs;
//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


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

 
J

Jonathan Roberts

Steve Le Monnier said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top