[英]C# Drag and Drop attached file from outlook email
I want to use/read attached files from an outlook email into a WinForm solution.我想将 Outlook 电子邮件中的附加文件使用/读取到 WinForm 解决方案中。
Ex: the email has a TXT file attached;例如:电子邮件附加了 TXT 文件; I want to perform a Drag&Drog of the TXT file into the WinForm and read the TXT at the same time.
我想将 TXT 文件拖放到 WinForm 中并同时读取 TXT。
This is an old question, but I'll provide another answer anyhow that doesn't involve using the Outlook objects.这是一个老问题,但无论如何我都会提供另一个不涉及使用 Outlook 对象的答案。
This URL provides working code that is about 13 years old, but still seems to work, on how to handle the "FileGroupDescriptor" and "FileContents" data that Outlook passes to the DropDrop event. 这个URL 提供了大约 13 年前的工作代码,但似乎仍然有效,关于如何处理 Outlook 传递给 DropDrop 事件的“FileGroupDescriptor”和“FileContents”数据。 Just in case that link dies, here is the relevant code, copy/pasted directly:
以防万一链接失效,这里是相关代码,直接复制/粘贴:
DragEnter event: 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 event:拖放事件:
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 !
}
}
Note that this code doesn't Dispose
of objects that it should, such as the MemoryStream
and FileStream
objects.请注意,此代码不会
Dispose
它应该Dispose
的对象,例如MemoryStream
和FileStream
对象。
You can get the running Outlook instance by using the GetActiveObject method which allows to obtain a running instance of the specified object from the running object table (ROT).您可以使用GetActiveObject方法获取正在运行的 Outlook 实例,该方法允许从运行对象表 (ROT) 中获取指定对象的运行实例。 Then you can automate Outlook to get the currently selected or opened item from which an attachment might be dragged.
然后,您可以自动化 Outlook 以获取可能从中拖动附件的当前选定或打开的项目。 See C# app automates Outlook (CSAutomateOutlook) for the sample code.
有关示例代码,请参阅C# 应用程序自动化 Outlook (CSAutomateOutlook) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.