Solved

Extract Attachment from Exchange server in C# VS 2008

Posted on 2010-11-23
10
803 Views
Last Modified: 2013-12-17
Hi there,

this sounds simple, but after googling to much my head has gone google.

I need to write a service that will scan an email box for new emails and pull out the attachments. The I will unzip them and process them. Sounds simple.

I have use .net to send emails but never to attach to an inbox and check for emails.

What is the simplest and easy way to do this... bearing in mind that the email account will be in my organisation that uses Outlook. I have seen some people use 3rd party POP3 libaries etc but surely this can be done by straight forward .net libaries.

I was going to test this by connecting to my own account and testing so any sample code on this would be great.

Much appreciated.. mick
0
Comment
Question by:MickyMc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
10 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 34195559
0
 

Author Comment

by:MickyMc
ID: 34195737
thanks emoreau..

I put some code together and just curious why you dont use the Interop.Outlook like below

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Outlook.Application outlook = new Outlook.ApplicationClass();
            Outlook.NameSpace ns = outlook.GetNamespace("Mapi");

            object _missing = Type.Missing;
            ns.Logon(_missing, _missing, false, true);


            Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

            int unread = inbox.UnReadItemCount;

            foreach (Outlook.MailItem mail in inbox.Items)
            {
                String subject;

                if (mail.Attachments.Count > 0)
                {
                    if (mail.Subject.Length > 20)
                        subject = mail.Subject.Substring(0, 20) + "..";
                    else
                        subject = mail.Subject;

                    dgvEmails.Rows.Add(mail.SenderName, subject, mail.ReceivedTime, mail.UnRead, mail.Attachments.Count);
                    dgvEmails.Refresh();
                    Application.DoEvents();
                }
            }                        
        }

Open in new window


I'm just reading through my outlook inbox and putting the results in a grid. However I get an exception on this for loop..
foreach (Outlook.MailItem mail in inbox.Items)

Open in new window

It tells me invalid cast... but it works for most mails and only throws an exception on some... any ideas
0
 

Author Comment

by:MickyMc
ID: 34196911
OK so it seems that when you iterate through the MailItems there can be Calendar items in your inbox and when you try to cast them... Exception.

Instead you have to iteratie through all the items and try and cast them to an Mail Item.

So back to the question ... is this the best approach  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 34204973
if you are using Exchange 2007 & 2010, the the EWS API is quite easy to use. http://msdn.microsoft.com/en-us/library/dd637749(EXCHG.80).aspx


0
 

Author Comment

by:MickyMc
ID: 34208287
robberbaron, you could be on the money here. This actually looks good. My only beef is that I cant see how I can specify an email server and login details. I used Interop.Outlook and found that it takes the creditentials from the client Outlook on my destop and just ignores the login creditentials in the api. I took emoreau advice and used http://www.lesnikowski.com/mail/  which is a great little application for the price. I could specify the server and get different users to login into their accounts and pull back the emails. However I'm not sure if my company will allow 3rd party controls to be used other than Microsoft.

So if you can let me know if this supports connections to various exchange servers with differential creditals the points are yours..
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 34210017
what do you want to do exactly? if you login using different credentials, yes you will be able to access what those credentials gives you access to.
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 250 total points
ID: 34210377
can certainly connect using various credentials.

All of my coding has been against PublicFolders, where i use a "service account" that can access the entire store.
But impersonation is available too.

see http://msdn.microsoft.com/en-us/library/dd633680%28EXCHG.80%29.aspx for both options.
 This links to the EWS documentation that has a number of useful examples.
0
 

Author Closing Comment

by:MickyMc
ID: 34225363
thanks Gents, I'm splitting the points as both answers where of great help so it comes down to a matter of preference. Thanks for all your help...

regards Mick
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This article explains how to install and use the NTBackup utility that comes with Windows Server.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question