Ejemplo n.º 1
0
        private void AddAttachmentsMenu(Button b, Outlook.MailItem mailItem)
        {
            ContextMenuStrip menuStrip = b.ContextMenuStrip;

            if (null == menuStrip)
            {
                menuStrip          = new ContextMenuStrip();
                b.ContextMenuStrip = menuStrip;
            }
            Outlook.Attachments attachments = mailItem.Attachments;
            if (attachments.Count == 0)
            {
                ToolStripMenuItem noAttachmentsItem = new ToolStripMenuItem();
                noAttachmentsItem.Text      = "(No Attachments)";
                noAttachmentsItem.ForeColor = Color.DarkGray;
                noAttachmentsItem.Enabled   = true;
                noAttachmentsItem.Click    += new System.EventHandler(this.NoAttachmentsMenuItem_Click);
                menuStrip.Items.Add(noAttachmentsItem);
            }
            else
            {
                ToolStripMenuItem attachmentsItem = new ToolStripMenuItem();
                attachmentsItem.Text = "Attachments";
                menuStrip.Items.Add(attachmentsItem);

                // make the save all attachments item and pass info through Tag
                ToolStripMenuItem saveAllAttachmentItem = new ToolStripMenuItem();
                saveAllAttachmentItem.Text   = "Save All";
                saveAllAttachmentItem.Click += new System.EventHandler(this.SaveAllAttachmentsMenuItem_Click);
                MailItemAttachments mias = new MailItemAttachments();
                foreach (Outlook.Attachment att in attachments)
                {
                    mias.Add(new MailItemAttachment(mailItem, att));
                }
                saveAllAttachmentItem.Tag = mias;
                attachmentsItem.DropDownItems.Add(saveAllAttachmentItem);

                // make an entry for each attachment to be saved individually and pass info through Tag
                foreach (Outlook.Attachment att in attachments)
                {
                    ToolStripMenuItem attachmentItem = new ToolStripMenuItem();
                    attachmentItem.Text = att.DisplayName;

                    ToolStripMenuItem saveAttachmentItem = new ToolStripMenuItem();
                    saveAttachmentItem.Text   = "Save";
                    saveAttachmentItem.Tag    = new MailItemAttachment(mailItem, att);
                    saveAttachmentItem.Click += new System.EventHandler(this.SaveAttachmentMenuItem_Click);
                    attachmentItem.DropDownItems.Add(saveAttachmentItem);

                    ToolStripMenuItem saveAndOpenAttachmentItem = new ToolStripMenuItem();
                    saveAndOpenAttachmentItem.Text   = "Save and Open";
                    saveAndOpenAttachmentItem.Tag    = new MailItemAttachment(mailItem, att);
                    saveAndOpenAttachmentItem.Click += new System.EventHandler(this.SaveAndOpenAttachmentMenuItem_Click);
                    attachmentItem.DropDownItems.Add(saveAndOpenAttachmentItem);

                    attachmentsItem.DropDownItems.Add(attachmentItem);
                }
            }
        }
Ejemplo n.º 2
0
        public void SaveAllAttachmentsMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem senderMenuItem = sender as ToolStripMenuItem;

            if (senderMenuItem != null)
            {
                FolderBrowserDialog fbd           = new FolderBrowserDialog();
                DialogResult        result        = fbd.ShowDialog();
                string foldername                 = fbd.SelectedPath;
                MailItemAttachments       mias    = (MailItemAttachments)senderMenuItem.Tag;
                List <MailItemAttachment> miaList = mias.GetMIAs();
                foreach (MailItemAttachment mia in miaList)
                {
                    Outlook.Attachment att = mia.GetAttachment();
                    Outlook.MailItem   mi  = mia.GetMailItem();
                    logger.Debug("would save attachment : " + att.FileName + " for " + mi.EntryID + NL);
                    String path = Path.Combine(foldername, att.FileName);
                    att.SaveAsFile(path);
                    Backend.AddResource(Utils.RESOURCE_TYPE_FILE, path);
                    Utils.TagResourceForMailItem(mi.EntryID, path);
                }
            }
        }