none
Custom Form - Selected Mail Item (Attachments) RRS feed

  • Question

  • Hello,

    I have created a custom form which I want to display when the user selects an individual mail item.

    At the moment, it displays To: From: CC: and Subject etc.

    But on that form.cs how can I also display any attachments for that particular mail.

    Thursday, November 26, 2015 12:38 PM

Answers

  • >>>So if I was to use a Listbox, could you give me a code example?

    According to your descritpion, you could refer to below steps:

    Right click->Add New Item...->Outlook Form Region then add Adjoining Form Region like below:

    then add ListBox to FormRegionXXX like below:

    View Code->FormRegionXXX.cs then add code like below:

    private void FormRegionAttachmentsList_FormRegionShowing(object sender, System.EventArgs e)
    {
         if (this.OutlookItem is Outlook.MailItem)
         {
              Outlook.MailItem mailItem = (Outlook.MailItem)this.OutlookItem;
    
              if (mailItem.Attachments.Count > 0)
              {
                  foreach (var obj in mailItem.Attachments)
                  {
                      lstAttachments.Items.Add(((Outlook.Attachment)obj).FileName);
                  }
              }
         }
    }

    For more information, click here to refer about How to: Access the Outlook Item that Displays the Form Region


    Monday, November 30, 2015 4:51 AM

All replies

  • Hello Darryl,

    Do you develop an Outlook Form Region? Did you use System.Windows.Forms controls for diaplying To, CC and etc. fields? If so, you can use any .Net control for displaying attachments, for example ListBox,

    Thursday, November 26, 2015 12:58 PM
  • Thanks Eugene,

    So if I was to use a Listbox, could you give me a code example?

    Thanks :)

    Friday, November 27, 2015 11:49 AM
  • >>>So if I was to use a Listbox, could you give me a code example?

    According to your descritpion, you could refer to below steps:

    Right click->Add New Item...->Outlook Form Region then add Adjoining Form Region like below:

    then add ListBox to FormRegionXXX like below:

    View Code->FormRegionXXX.cs then add code like below:

    private void FormRegionAttachmentsList_FormRegionShowing(object sender, System.EventArgs e)
    {
         if (this.OutlookItem is Outlook.MailItem)
         {
              Outlook.MailItem mailItem = (Outlook.MailItem)this.OutlookItem;
    
              if (mailItem.Attachments.Count > 0)
              {
                  foreach (var obj in mailItem.Attachments)
                  {
                      lstAttachments.Items.Add(((Outlook.Attachment)obj).FileName);
                  }
              }
         }
    }

    For more information, click here to refer about How to: Access the Outlook Item that Displays the Form Region


    Monday, November 30, 2015 4:51 AM
  • Hi David,

    That's perfect! One last thing - How can the user open the attachment by clicking on each one in the ListBox?

    Thanks,

    Darryl :)

    Monday, November 30, 2015 11:30 AM
  • You can use the SelectedIndexChanged event of the LitBox class. It is fired when the SelectedIndex property or the SelectedIndices collection has changed.
    Monday, November 30, 2015 7:00 PM
  • Thanks Eugene, that was the event I was planning on using. But how would I save the selected attachment.

    If I had a listbox with the names of the attachments in (as above).

    Monday, November 30, 2015 7:58 PM
  • You can find the corresponding attachment in the Attachments collection (see the corresponding property of the MailItem class).
    Monday, November 30, 2015 8:01 PM
  • Could you give me an example? :)

    Monday, November 30, 2015 10:39 PM
  • >>>Could you give me an example? :)

    You could add SelectedIndexChanged event of the LitBox class, then refer to below code:

    Outlook.Attachments attachments = null;
    private void FormRegionAttachmentsList_FormRegionShowing(object sender, System.EventArgs e)
    {
          if (this.OutlookItem is Outlook.MailItem)
          {
               Outlook.MailItem mailItem = (Outlook.MailItem)this.OutlookItem;
    
               attachments = mailItem.Attachments;
    
               if (attachments.Count > 0)
               {
                   foreach (var obj in attachments.Cast<Outlook.Attachment>())
                   {
                       lstAttachments.Items.Add(((Outlook.Attachment)obj).FileName);
                   }
               }
           }
    }
    private void lstAttachments_SelectedIndexChanged(object sender, EventArgs e)
    {
         if (lstAttachments.SelectedIndex >= 0)
         {
            attachments[lstAttachments.SelectedIndex + 1].SaveAsFile(@"D:\\" + attachments[lstAttachments.SelectedIndex + 1].FileName);
         }
    }
    

    Tuesday, December 1, 2015 2:11 AM
  • Thanks David.

    I am getting this error:

    Additional information: Cannot save the attachment. You don't have appropriate permission to perform this operation.

    Is there no way for the user to choose the location to avoid this. Like a save dialog?

    Tuesday, December 1, 2015 2:46 AM
  • >>>Additional information: Cannot save the attachment. You don't have appropriate permission to perform this operation.

    Base on this error, I suggest that you could try another folder and also test saving that particular attachment manually.

    >>>Is there no way for the user to choose the location to avoid this. Like a save dialog?

    No, I suggest that you could use FolderBrowserDialog to choose the location, you could refer to below code:

    FolderBrowserDialog folderDlg = null;
    
    private void btnSaveAttachment_Click(object sender, EventArgs e)
    {
         string saveAttachmentPath = null;
         if (lstAttachments.SelectedIndex >= 0)
         {
             folderDlg = new FolderBrowserDialog();
             folderDlg.RootFolder = Environment.SpecialFolder.MyComputer;
             if(folderDlg.ShowDialog()==DialogResult.OK)
             {
                  saveAttachmentPath = folderDlg.SelectedPath + "\\" + attachments[lstAttachments.SelectedIndex + 1].FileName;
                  attachments[lstAttachments.SelectedIndex + 1].SaveAsFile(saveAttachmentPath);
             }
         }
    }
    

    Tuesday, December 1, 2015 8:39 AM