c#从outlook电子邮件中拖放附加文件

本文关键字:文件 拖放 outlook 电子邮件 | 更新日期: 2023-09-27 18:16:43

我想在WinForm解决方案中使用/读取outlook电子邮件中的附件文件。

示例:电子邮件附带一个TXT文件;我想执行一个拖拽& quot;Drog TXT文件到WinForm和读取TXT在同一时间

c#从outlook电子邮件中拖放附加文件

这是一个老问题,但我将提供另一个不涉及使用Outlook对象的答案。

这个URL提供了大约13年前的工作代码,但似乎仍然有效,关于如何处理Outlook传递给DropDrop事件的"FileGroupDescriptor"answers"FileContents"数据。为了防止链接失效,下面是相关代码,直接复制/粘贴:

DragEnter事件:

private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
  // for this program, we allow a file to be dropped from Explorer
  if (e.Data.GetDataPresent(DataFormats.FileDrop))
  {  e.Effect = DragDropEffects.Copy;}
  //    or this tells us if it is an Outlook attachment drop
  else if (e.Data.GetDataPresent("FileGroupDescriptor"))
  {  e.Effect = DragDropEffects.Copy;}
  //    or none of the above
  else
  {  e.Effect = DragDropEffects.None;}
}

DragDrop事件:

private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
  string [] fileNames = null;
  try
  {
    if ( e.Data.GetDataPresent(DataFormats.FileDrop,false) == true)
    {
      fileNames = (string []) e.Data.GetData(DataFormats.FileDrop);
      // handle each file passed as needed
      foreach( string fileName in fileNames)
      {
        // do what you are going to do with each filename
      }
    }
    else if (e.Data.GetDataPresent("FileGroupDescriptor"))
    {
      //
      // the first step here is to get the filename
      // of the attachment and
      // build a full-path name so we can store it
      // in the temporary folder
      //
      // set up to obtain the FileGroupDescriptor
      // and extract the file name
      Stream theStream = (Stream) e.Data.GetData("FileGroupDescriptor");
      byte [] fileGroupDescriptor = new byte[512];
      theStream.Read(fileGroupDescriptor,0,512);
      // used to build the filename from the FileGroupDescriptor block
      StringBuilder fileName = new StringBuilder("");
      // this trick gets the filename of the passed attached file
      for(int i=76; fileGroupDescriptor[i]!=0; i++)
      {  fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));}
      theStream.Close();
      string path = Path.GetTempPath();
          // put the zip file into the temp directory
      string theFile = path+fileName.ToString();
          // create the full-path name
      //
      // Second step:  we have the file name.
      // Now we need to get the actual raw
      // data for the attached file and copy it to disk so we work on it.
      //
      // get the actual raw file into memory
      MemoryStream ms = (MemoryStream) e.Data.GetData(
          "FileContents",true);
      // allocate enough bytes to hold the raw data
      byte [] fileBytes = new byte[ms.Length];
      // set starting position at first byte and read in the raw data
      ms.Position = 0;
      ms.Read(fileBytes,0,(int)ms.Length);
      // create a file and save the raw zip file to it
      FileStream fs = new FileStream(theFile,FileMode.Create);
      fs.Write(fileBytes,0,(int)fileBytes.Length);
      fs.Close();  // close the file
      FileInfo tempFile = new FileInfo(theFile);
      // always good to make sure we actually created the file
      if ( tempFile.Exists == true)
      {
        // for now, just delete what we created
        tempFile.Delete();
      }
      else
      {  Trace.WriteLine("File was not created!");}
    }
  }
  catch (Exception ex)
  {
    Trace.WriteLine("Error in DragDrop function: " + ex.Message);
    // don't use MessageBox here - Outlook or Explorer is waiting !
  }
}

请注意,这段代码没有Dispose对象,它应该,如MemoryStreamFileStream对象。

您可以通过使用GetActiveObject方法获得正在运行的Outlook实例,该方法允许从运行对象表(ROT)中获取指定对象的运行实例。然后,您可以使Outlook自动获取当前选择或打开的项目,并从中拖动附件。示例代码参见c# app automates Outlook (CSAutomateOutlook)。