none
save Outlook E-Mail attachments from 2007

    Question

  •  private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.NewMail +=new Outlook.ApplicationEvents_11_NewMailEventHandler(Application_NewMail);
                
                
            }
            public void Application_NewMail()
            {
                 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 != null)
                        {
                            if (newEmail.Attachments.Count > 0)
                            {
                                for (int i = 1; i <= newEmail
                                   .Attachments.Count; i++)
                                {
                                    newEmail.Attachments[i].SaveAsFile
                                        (@"C:\TestFileSave\" +
                                        newEmail.Attachments[i].FileName);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errorInfo = (string)ex.Message
                        .Substring(0, 11);
                    if (errorInfo == "Cannot save")
                    {
                       System.Windows.Forms.MessageBox.Show(@"Create Folder C:\TestFileSave");
                    }
                }
            }
            
    • Moved by Mike Feng Wednesday, December 12, 2012 2:55 AM (From:.NET Platform Architecture Development Discussions)
    Monday, December 10, 2012 9:40 AM

Answers

  • Hi Krish233,

    I've tried your code. It works fine on my side. 

    You can try to disable all other add-ins and then send an email with attachments to the account in Outlook, alos you can try Application.NewMailEx Event.


    Regards,
    Fermin
    What's life without whimsy?


    • Marked as answer by krish233 Wednesday, December 12, 2012 7:16 AM
    • Unmarked as answer by krish233 Wednesday, December 12, 2012 7:16 AM
    • Marked as answer by krish233 Wednesday, December 12, 2012 7:17 AM
    • Edited by Fermin Wenlock Wednesday, December 12, 2012 9:03 AM
    Wednesday, December 12, 2012 6:13 AM

All replies

  • You have not mentioned your problems statement. Are you geting any Error ? Is your event not firing ?

    In C# Array/Collections index start with Zero. Make sure you’re for loop start with 0 rather than 1 as below.

     for (int i = 0; i < newEmail
                 .Attachments.Count; i++)
     {
     newEmail.Attachments[i].SaveAsFile .. 

    It's based on my experience that your issue will better supported at @  Outlook for Developers Forum. I would recommend you to make our question specific and post your question to the said forum.


    Lingaraj Mishra



    Monday, December 10, 2012 10:28 AM
  • i tried in that way also but attachment count is allways 0  so..........it's is not ging into the for loop
    • Marked as answer by krish233 Monday, December 10, 2012 11:53 AM
    • Unmarked as answer by krish233 Monday, December 10, 2012 11:53 AM
    Monday, December 10, 2012 10:57 AM
  • Are you sure your new mail got an attachment? Try posting your question to the said forum.


    Lingaraj Mishra

    Monday, December 10, 2012 4:10 PM
  • Hi Krish233,

    I've tried your code. It works fine on my side. 

    You can try to disable all other add-ins and then send an email with attachments to the account in Outlook, alos you can try Application.NewMailEx Event.


    Regards,
    Fermin
    What's life without whimsy?


    • Marked as answer by krish233 Wednesday, December 12, 2012 7:16 AM
    • Unmarked as answer by krish233 Wednesday, December 12, 2012 7:16 AM
    • Marked as answer by krish233 Wednesday, December 12, 2012 7:17 AM
    • Edited by Fermin Wenlock Wednesday, December 12, 2012 9:03 AM
    Wednesday, December 12, 2012 6:13 AM
  • what exactly does not work with your code? and contrary to what Lingaraj Mishra said - COM based collections start counting from 1, so this is ok.

    Wednesday, December 12, 2012 7:11 AM