473,378 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Help Looping Through my Inbox and extracting attachments

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
Aug 2 '06 #1
8 3164
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
Aug 2 '06 #2
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" <pi**********@hotmail.comwrote in message
news:11**********************@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

Aug 2 '06 #3
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" <pi**********@hotmail.comwrote in message
news:11**********************@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
Aug 2 '06 #4
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" <pi**********@hotmail.comwrote in message
news:11*********************@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" <pi**********@hotmail.comwrote in message
news:11**********************@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

Aug 2 '06 #5
Steve,
Just found this on MS - any good?

http://msdn.microsoft.com/library/de...rp05152003.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" <pi**********@hotmail.comwrote in message
news:11*********************@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" <pi**********@hotmail.comwrote in message
news:11**********************@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
Aug 2 '06 #6
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" <pi**********@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Steve,
Just found this on MS - any good?

http://msdn.microsoft.com/library/de...rp05152003.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" <pi**********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegr oups.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" <pi**********@hotmail.comwrote in message
news:11**********************@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


Aug 2 '06 #7
No worries. Glad i could help.

James

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

http://msdn.microsoft.com/library/de...rp05152003.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" <pi**********@hotmail.comwrote in message
news:11*********************@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" <pi**********@hotmail.comwrote in message
news:11**********************@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
Aug 2 '06 #8

"Steve Le Monnier" <st*********@no.spam.hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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.

Aug 2 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dafella | last post by:
The following is code from Xeoport. It is suppose to access my pop3 account and load email into Mysql database. It's not inserting and there is no log or anything to tell me why. Here is the...
27
by: gRizwan | last post by:
Hello all, We have a problem on a webpage. That page is sent some email data in base64 format. what we need to do is, decode the base64 data back to original shape and extract attached image...
0
by: Bob Bykerk | last post by:
Hi guys, I am integrating the outlook inbox into a personnel database and have successfully linked the table. I can see where an e-mail has attachments, but can't get to them. Has anyone done...
0
by: Saurabh Sharma | last post by:
Hi I have recieved a mails in which attachments are of form .eml and .msg and those attachmenst further have same attachments and at the end there are doc attachments. I want to extract these...
1
by: tom north | last post by:
What I am after would be a VB script perhaps using CDO to access the inbox of a mailbox, and push or pull messages in or out to a flat file directory on disk -- Tom North
0
by: Prash | last post by:
I have a requirement where in i need to download attachments (doc files) from the email inbox. I tried using axWebBrowser1 control embedded on the form. I allow the user to login to the inbox and...
1
by: azmir10 | last post by:
Hi everyone, I'm currently doing an interface program of 'near real time updating database' involving Visual Basic 6.0 and MapObjects. I'm having a problem in extracting attachments from Outlook. Is...
3
by: GayathriP | last post by:
This article discusses about how can we access the inbox through MAPI using C# .NET. It also discusses about how to store the attachment in the local directory, how to create a folder in the inbox ...
2
by: pnd1234 | last post by:
Hi, i am doing a project which represent message attachments in a xml view.I currently implemented the system in a tree view.Like if a message contains word file, zip file,image file , swf file...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.