1

在这里,我将创建一个 Outlook 添加,其中使用 C# 从 Outlook 中提取附件。

我使用插件和on_click事件在 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");
    }
  }
}
4

3 回答 3

2

创建一个 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);
于 2013-05-16T06:59:26.220 回答
0

我会考虑挂钩 Explorer.Selection 更改事件:http: //msdn.microsoft.com/en-us/library/office/ff869813.aspx

于 2013-05-16T09:01:22.050 回答
0

您是否有关于查找所选电子邮件或在当前电子邮件中选择附件的问题?

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

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.
    }
}
于 2013-05-16T09:14:20.160 回答