C# 是否从outlook中提取所选邮件的附件?

C# 是否从outlook中提取所选邮件的附件?,c#,outlook-addin,C#,Outlook Addin,在这里,我将创建一个Outlook加载项,它使用C从Outlook中提取附件 我使用外接程序在Outlook上放置了一个按钮,并在单击事件中,我在下面调用了此方法;代码运行良好。它正在提取Outlook收件箱中的所有附件,但我只需要从鼠标中选择的附件 有人能帮我解决这个问题吗 private void ThisApplication_NewMail() { Outlook.MAPIFolder inBox = this.Application.ActiveExplorer().Session

在这里,我将创建一个Outlook加载项,它使用C从Outlook中提取附件

我使用外接程序在Outlook上放置了一个按钮,并在单击事件中,我在下面调用了此方法;代码运行良好。它正在提取Outlook收件箱中的所有附件,但我只需要从鼠标中选择的附件

有人能帮我解决这个问题吗

private void ThisApplication_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");
    }
  }
}
创建FormRegion控件并将其插入outlook消息窗口

然后,当您单击收件箱中的邮件时,您可以使用以下内容获取邮件类:

private void FormRegionMessageClassArchivadoFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {

                Outlook.MailItem item = (Outlook.MailItem)e.OutlookItem;     
                 if (item.Attachments.Count > 0)
                 {
                                int attachRestantes = item.Attachments.Count;

                                for (int j = attachRestantes; j >=1; j--)
                                {
                                    //get attachments
                                }           

                 }     
            }
编辑:

要获取作为字节的附件内容,请使用以下代码

 //microsoft schema to get the attachment content
 private string AttachSchema="http://schemas.microsoft.com/mapi/proptag/0x37010102";

  Outlook.PropertyAccessor pacc = item.Attachments[j].PropertyAccessor;
  byte[] filebyte = (byte[])pacc.GetProperty(AttachSchema);

我将考虑挂接到资源管理器。选择更改事件:

您的问题是查找所选电子邮件,还是在当前电子邮件中选择附件

我们提取邮箱中选定的电子邮件,然后提示用户选择要保存在自定义表单中的附件。下面是我们提取电子邮件用户选择的例程

Outlook.Explorer curExplorer = OutlookApplication.ActiveExplorer();
Outlook.NameSpace curNameSpace = OutlookApplication.GetNamespace("MAPI");
Outlook.MAPIFolder curFolder = curExplorer.CurrentFolder;
if (curExplorer.Selection != null && curExplorer.Selection.Count > 0)
{
    // get mails
    _lstMailItems = new List<Outlook.MailItem>();
    try
    {
        foreach (Outlook.MailItem curMailItem in curExplorer.Selection)
        {
            // modification on mail items in plugin are repercuted in Outlook
            _lstMailItems.Add(curMailItem);
        }
    }
    catch (COMException exc)
    {
        // log, selected item is not an email.
    }
}

嗨,约翰·威伦斯。你编辑什么?它在outlook 2007中不起作用。在outlook 2010中,它运行良好。@CarlosLanderas