C# copy outlook emails to folder

pauledwardian
pauledwardian used Ask the Experts™
on
How is it possible to copy (not move)  messages from outlook by C# to a folder whenever the programs is running?

Paul
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
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)?

Author

Commented:
.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
Most Valuable Expert 2012
Top Expert 2008

Commented:
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Most Valuable Expert 2012
Top Expert 2008
Commented:
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

Most Valuable Expert 2012
Top Expert 2008

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

Open in new window

Most Valuable Expert 2012
Top Expert 2008

Commented:
using System.Text.RegularExpressions;
using Redemption;

Author

Commented:
where should I donwload Redemption refferance from?
Most Valuable Expert 2012
Top Expert 2008

Commented:
There is a Download link on the Redemption web site:

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

Author

Commented:
Is it free?
Most Valuable Expert 2012
Top Expert 2008

Commented:
Yes, the Developer Edition is free.  If you have to distribute, there is a fee.

Author

Commented:
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);

Author

Commented:
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

Most Valuable Expert 2012
Top Expert 2008

Commented:
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).

Author

Commented:
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?
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.

Author

Commented:
I've requested that this question be closed as follows:

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

for the following reason:

Thanks!
Most Valuable Expert 2012
Top Expert 2008

Commented:
I think that there was some help from me, don't ya think?

Author

Commented:
Yes, I accidentally selected my solution :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial