none
Looping up wrongly Utlook Plugin RRS feed

  • Question

  • Hi,

    This is my Code, This code works when a email with attachemnt name as "LogicView Report" it Gets downloaded into desired location.

    My Issue is when 2 or 3 mails, All reports with attachments should be downloaded, but when i check the download folder I am seeing only report from first Email, It is looping 4 times (! second, third email and again first email)and i am seeing  only first downloaded report . Can some help me out? 

     public partial class ThisAddIn
        {
            Outlook.NameSpace outlookNameSpace;
            Outlook.MAPIFolder inbox;
            Outlook.Items items;
            int i;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                outlookNameSpace = this.Application.GetNamespace("MAPI");
                inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                items = inbox.Items;
                items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
            }

            private void items_ItemAdd(object Item)
            {

                Outlook.Items inboxitems;
                const string destinationDirectory = @"C:\Users\c433\Desktop\Adi\";
                Outlook.MailItem newEmail = null;
                inboxitems = inbox.Items.Restrict("[Unread] = true");

                try
                {
                  foreach (object collectionItem in inboxitems)
                    {
                             newEmail = collectionItem as Outlook.MailItem;                
                      for (i=1; i <= newEmail
                                   .Attachments.Count; i++)
                                {

                                    if (newEmail.Attachments[i].FileName.Contains("LogicView Report"))
                                    {
                                        //   String Des= destinationDirectory.Remove(0, 1);
                                        newEmail.Attachments[i].SaveAsFile(destinationDirectory +
                                            newEmail.Attachments[i].FileName);
                                        //  MessageBox.Show("Hurry");
                                    }
                                                                   
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("" + ex);
                }
            }

    Saturday, March 14, 2015 11:22 PM

Answers

All replies

  • Why are you looking for the unread emails in the Inbox every time Items.ItemAdd event fires? It makes sense to do that once on the addin's startup to process all unread items accumulated while Outlook was not running (in case of Exchange), but when the ItemAdd event fires, why not use the item passed to your event handler as a parameter?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, March 14, 2015 11:26 PM
  • Can you post a example through my code as I am new to addin and C#

    My task is simple when email arrives it must download to a particular location ; if i get 2 emails at a time both must be downloaded that it.

    I have tried this example but some how it is not working...

    https://msdn.microsoft.com/en-us/library/ms268754.aspx

    Saturday, March 14, 2015 11:43 PM
  • private void items_ItemAdd(object Item) { Outlook.MailItem newEmail = Item as Outlook.MailItem if (newEmail != null) { foreach (Outlook.Attachment attach in newMail.Attachments) { if (attach .FileName.Contains("LogicView Report"))
              {
              attach.SaveAsFile(destinationDirectory + attach.FileName);
             }

    }

    }

    }



    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!



    Sunday, March 15, 2015 12:32 AM
  • Hi  Dmitry,

    Thanks for your reply, But

    So If i Send 2 emails with attachemnt of LogicView Report ,I am seeing report data only from 2 email . I need the first email (Latest Report data)

    Here is the image

    https://social.msdn.microsoft.com/Forums/getfile/622846

    This is my current code..

    namespace OutlookAddIn1
    {

        public partial class ThisAddIn
        {
            Outlook.NameSpace outlookNameSpace;
            Outlook.MAPIFolder inbox;
            Outlook.Items items;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                outlookNameSpace = this.Application.GetNamespace("MAPI");
                inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                items = inbox.Items;
                items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
            }
            private void items_ItemAdd(object Item)
    {
                
                const string destinationDirectory = @"C:\Users\c200433\Desktop\Adi\";
                Outlook.MailItem newEmail = Item as Outlook.MailItem;
       if (newEmail != null)
       { 
          foreach (Outlook.Attachment attach 
              in newEmail.Attachments)
          {
              if (attach .FileName.Contains("LogicView Issue Report"))
              {
                 attach.SaveAsFile(destinationDirectory + attach.FileName);
              } 
           }
         }
    }
    Sunday, March 15, 2015 1:25 AM
  • Then it will be your responsibility to check to decide which email you want to save. Or you can make the filename unique and save both attachments.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, March 15, 2015 2:49 AM
  • It looks like the file is overwritten each time because you save the attachment with the same filename. Try to use a date time marker for differentiating attachments on the disk.

    attach.SaveAsFile(destinationDirectory + newMail.Recieved+attach.FileName);

    Sunday, March 15, 2015 3:56 AM
  • newMail.Received will be converted to a string using the system short date date/time format, which might include "/" or "\", which are obviously invalid in a file name.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, March 15, 2015 4:06 AM
  • Yes u are right it is overwrting... as

    supoose

    IF i have Email in order as EMAIl 1 and EMail 2 it is getting downloaded as 

    attachment of Email 1 then attachment Email 2 so when i check download folder it is showing Email 2 attachment. I need Email 1 attachment (The email the is present at the begining of the mail)how do i restrict it?

    Thanks

    Sunday, March 15, 2015 5:24 AM
  • So what is your condition? Received date? Sender name? Or something else/

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, March 15, 2015 5:49 AM
  • It was just a raw sketch. Anyway, criticism is not the solution.
    Sunday, March 15, 2015 12:36 PM
  • What code do you use to save attachments with unique names?
    Sunday, March 15, 2015 12:38 PM
  • Split the file name into name and extension. Loop over integer i checking on each step  if a file in the form "name(i).ext" exists.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, March 15, 2015 3:32 PM