none
i am not able save my outlook email attachments to my localfolder? RRS feed

  • Question

  • Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
                    .Session.GetDefaultFolder(Outlook
                    .OlDefaultFolders.olFolderInbox);
                Outlook.Items inBoxItems = inBox.Items;
                Outlook.MailItem newEmail = null;
               // inBoxItems = inBoxItems.Restrict("[Unread] = true");
                try
                {
                    foreach (object collectionItem in inBoxItems)
                    {

                       newEmail = collectionItem as  Outlook.MailItem;
                        if (newEmail.Attachments.Count>=1)
                        {

                            for (int i = 0; i <= newEmail.Attachments.Count; i++)
                            {
                                newEmail.Attachments[i].SaveAsFile(@"C:\TestFileSave\" +newEmail.Attachments[i].FileName);
                            }

                        }
                    }
                }
                catch (System.Exception ex)
                {
                    string errorInfo = (string)ex.Message
                        .Substring(0, 11);
                    if (errorInfo == "Cannot save")
                    {
                        MessageBox.Show(@"Create Folder C:\TestFileSave");
                    }
                }
            }

    the count value is not increasing.........................even though mail have attachment

                                                    
    • Edited by krish233 Monday, December 10, 2012 10:54 AM
    Monday, December 10, 2012 10:53 AM

Answers

  • yaa finally i got it

    see the fallowing code ................

     private void Application_ItemLoad(object Item)
            {
                const string destinationDirectory = @"C:\TestFileSave";

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

                MAPIFolder inbox = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                Items inboxItems = inbox.Items;
                
                
                    foreach (object collectionItem in inboxItems)
                    {
                        MailItem newEmail = collectionItem as MailItem;
                        if (newEmail == null) continue;

                        if (newEmail.Attachments.Count > 0)
                        {
                            for (int i = 1; i <= newEmail.Attachments.Count; i++)
                            {
                                string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
                                newEmail.Attachments[i].SaveAsFile(filePath);
                            }
                        }
                    }
                }
                

    • Marked as answer by krish233 Monday, December 10, 2012 1:24 PM
    Monday, December 10, 2012 1:18 PM

All replies

  • what do you mean by count value? Count property of Attachments? or something else?
    Monday, December 10, 2012 12:26 PM
  • what do you mean by count value? Count property of Attachments? or something else?
    yes count value of attachment property only..........
    Monday, December 10, 2012 12:35 PM
  • by increasing you mean - does not reflect number of attachments that this mail has in inbox? each mail has the same count of attachments = 0?

    Monday, December 10, 2012 12:42 PM
  • one more thing, i just noted it right now - you should start your attachment loop from 1, not 0
    Monday, December 10, 2012 12:42 PM
  • yaa finally i got it

    see the fallowing code ................

     private void Application_ItemLoad(object Item)
            {
                const string destinationDirectory = @"C:\TestFileSave";

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

                MAPIFolder inbox = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                Items inboxItems = inbox.Items;
                
                
                    foreach (object collectionItem in inboxItems)
                    {
                        MailItem newEmail = collectionItem as MailItem;
                        if (newEmail == null) continue;

                        if (newEmail.Attachments.Count > 0)
                        {
                            for (int i = 1; i <= newEmail.Attachments.Count; i++)
                            {
                                string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
                                newEmail.Attachments[i].SaveAsFile(filePath);
                            }
                        }
                    }
                }
                

    • Marked as answer by krish233 Monday, December 10, 2012 1:24 PM
    Monday, December 10, 2012 1:18 PM
  • so now it works?
    Monday, December 10, 2012 1:23 PM
  • so now it works?

    s..s..

    Monday, December 10, 2012 1:24 PM