Solved

C# copy outlook emails to folder

Posted on 2012-03-23
18
1,099 Views
Last Modified: 2012-03-26
How is it possible to copy (not move)  messages from outlook by C# to a folder whenever the programs is running?

Paul
0
Comment
Question by:pauledwardian
  • 10
  • 8
18 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758080
What version of .NET do you have?

What version of Outlook are you using?

Windows Forms or WPF?

What do you mean by "folder" (file or Outlook)?
0
 

Author Comment

by:pauledwardian
ID: 37758246
.Net: 4.0 and 3.5
VS version: 2010
Outlook: 2010 and 2007
C# Windows Form
I mean a static address for a folder such as C:\All Emails so the program grabs the emails and copy them to the C:\All Emails from outlook. But, doesnt duplicate if it is already there.

Thank you,
Paul
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758545
I am using Redemption with C#, to access my Outlook.  I am always looking to figure how to do something that I haven't figured out before.  

Are you talking about from the current users Inbox to a folder?

Redemption
http://www.dimastr.com/redemption/home.htm
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37758636
Here is a Redemption example snippet:

        public void DownloadEmailToFolder(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            RDOFolder inbox = this.redemptionSession.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);

            List<RDOMail> mailItemList = inbox.Items.OfType<RDOMail>().ToList();

            foreach (RDOMail mail in mailItemList)
            {
                string fileName = Path.Combine(path, this.MakeValidFileName(mail.Subject));

                try
                {
                    mail.SaveAs(fileName);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Invalid path: " + fileName);
                }
            }
        }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758657
        private string MakeValidFileName(string input)
        {
            string pattern = "[?:/*]+";
            return Regex.Replace(input, pattern, "");
        }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758659
using System.Text.RegularExpressions;
using Redemption;
0
 

Author Comment

by:pauledwardian
ID: 37758884
where should I donwload Redemption refferance from?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37759046
There is a Download link on the Redemption web site:

Redemption
http://www.dimastr.com/redemption/download.htm
0
 

Author Comment

by:pauledwardian
ID: 37759367
Is it free?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 37759569
Yes, the Developer Edition is free.  If you have to distribute, there is a fee.
0
 

Author Comment

by:pauledwardian
ID: 37760117
Now its all good. Except I get this error for redemptionsession:


Error      1      'WindowsFormsApplication10.Form1' does not contain a definition for 'redemptionSession' and no extension method 'redemptionSession' accepting a first argument of type 'WindowsFormsApplication10.Form1' could be found (are you missing a using directive or an assembly reference?)      c:\users\comp\documents\visual studio 2010\Projects\WindowsFormsApplication10\WindowsFormsApplication10\Form1.cs      35      36      WindowsFormsApplication10


in this line:
RDOFolder inbox = this.redemptionSession.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
0
 

Author Comment

by:pauledwardian
ID: 37760209
This is how I did it. Can you double check plz: Also, does it also copy the files from the Inbox Sub-folder as well?

public void DownloadEmailToFolder(string path)
        {
           // Redemption.RDOSession Session = default(Redemption.RDOSession);
            dynamic Session = Activator.CreateInstance(Type.GetTypeFromProgID("Redemption.RDOSession"));
            Session.Logon();

 if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            RDOFolder inbox = Session.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);

            List<RDOMail> mailItemList = inbox.Items.OfType<RDOMail>().ToList();

            foreach (RDOMail mail in mailItemList)
            {
                string fileName = Path.Combine(path, this.MakeValidFileName(mail.Subject)+".msg");
                //if (mail.ReceivedTime.Equals("03 / 23 / 2012"))
                    
                    try
                {
                    mail.SaveAs(fileName);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Invalid path: " + fileName);
                }
                }
                
            
        }
        private string MakeValidFileName(string input)
        {
            string pattern = "[?:/*]+";
            return Regex.Replace(input, pattern, "");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DownloadEmailToFolder("C:\\emails");
        }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37760656
You should be able use early-binding, instead of Activator.CreateInstance.  The class that I have for an Outlook service is pretty big, and I just gave you a little piece of it.

Add a reference to Redemption, and use the typical style:

RDOSession session = new RDOSession();

Add a using statement at the top of the class:

using Redemption;

Also, I prefer to separate the behavior from the user interface (UI), so all the methods for Outlook/Redemption are in there own class (OutlookService).
0
 

Author Comment

by:pauledwardian
ID: 37761327
Does it also take care of the sub folders under the Inbox. Lets say if the user created couple of sub directory like Customers, Bills, etc..
Does the program grab emails from those directory as well? or does it only look into the Inbox folder?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37761594
It shouldn't do the sub-folders, since it just loops through the Items property for the folder, and does a SaveAs.  You would need to get a reference to each of the sub-folders, and repeat the same process.  That could be a recursive method call.
0
 

Author Comment

by:pauledwardian
ID: 37769570
I've requested that this question be closed as follows:

Accepted answer: 0 points for pauledwardian's comment #37760209

for the following reason:

Thanks!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37769571
I think that there was some help from me, don't ya think?
0
 

Author Closing Comment

by:pauledwardian
ID: 37769608
Yes, I accidentally selected my solution :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now