有没有办法在 C# 中不使用 x32 库来读取 .msg 文件?

问题描述 投票:0回答:1

我正在开发一个具有

PictureBox
且启用了
DragAndDrop
属性的表单。

我想要完成的是让用户能够从 Microsoft Outlook 应用程序中拖动电子邮件。

我搜索了很多解决方案,但大多数建议使用

Microsoft.Office.Interop.Outlook.MailItem
,但我读到它仅适用于 x32。

我的

DragEnter
事件看起来像这样:

private void DragDrop_DragEnter(object sender, DragEventArgs e)
{
   if (e.Data.GetDataPresent(DataFormats.UnicodeText))
   {
       e.Effect = DragDropEffects.Copy;
   }
   
   if (e.Data.GetDataPresent("FileGroupDescriptorW") &&
       e.Data.GetDataPresent("FileContents"))
   {
       e.Effect = DragDropEffects.Copy;
   }     
 }

如果我需要改变什么,请告诉我。

我尝试用

FileStream
读取消息,但存储的文件似乎是空的,这是代码:

// Get the .msg file contents as a stream
Stream msgFileStream = (Stream)e.Data.GetData("RenPrivateSourceFolder");

try
{
    // Create a FileStream for the target file
    using (FileStream targetFs = new FileStream(_defaultPath + ft.GetFileName(_ID.ToString(), _defaultPath, ".eml"), FileMode.Create, FileAccess.Write))
    {
        // Copy the .msg contents from the stream to the target file
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = msgFileStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            targetFs.Write(buffer, 0, bytesRead);
        }

        Console.WriteLine("File copied successfully.");
    }
}
catch (Exception ex)
{
     Console.WriteLine(ex.HResult);
}
finally
{
     msgFileStream.Close();
}

现在我已经提供了我手头的所有信息,有没有办法能够将文件从 Microsoft Outlook 拖到表单中并存储

.msg
文件而不使用此 x32 库?

c# .net outlook drag-and-drop mailmessage
1个回答
0
投票

无论您的代码是 32 位还是 64 位,无论您的应用程序的位数是多少,Microsoft.Office.Interop.Outlook.MailItem 都可以正常工作 - Outlook 是进程外 COM 服务器,可以跨任何位数的进程工作。

您当然可以在不使用 OOM 的情况下读取 MSG 文件的内容 - 有一些 C# 库可以为您执行此操作,例如https://github.com/Sicos1977/MSGReader

© www.soinside.com 2019 - 2024. All rights reserved.