错误:无法在Outlook中拖放xxx.msg拖放添加

时间:2013-11-16 05:00:57

标签: c# drag-and-drop outlook outlook-addin

我正在为outlook创建加载项。我在其中创建了拖放用户控件 当我拖动邮件时,一些邮件被提取并提供正确的信息,但在Inbox的一些邮件中给我的错误就像:

enter image description here

我正在使用以下代码来获取拖动邮件的信息:

private void DragNDropArea_DragDrop(object sender, DragEventArgs e)
{
    //wrap standard IDataObject in OutlookDataObject
    OutlookDataObject dataObject = new OutlookDataObject(e.Data);

    //get the names and data streams of the files dropped
    string[] filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
    MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents");

    this.label2.Text += "Files:\n";
    for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
    {
        try
        {
            //use the fileindex to get the name and data stream
            string filename = filenames[fileIndex];
            MemoryStream filestream = filestreams[fileIndex];
            this.label2.Text += "    " + filename + "\n";

            Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
            Outlook._NameSpace nameSpace = app.GetNamespace("MAPI");
            nameSpace.Logon(null, null, false, false);
            Outlook.Folder folder = (Outlook.Folder)app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

            //From this it gives me mentioned error...
            Outlook.MailItem msg = (Outlook.MailItem)app.CreateItemFromTemplate(filename, folder);
            string sender1 = msg.SenderEmailAddress;

            MessageBox.Show("Sender: \n" + msg.Sender.Name + "\n" + msg.Sender.Address);
            MessageBox.Show("Message Body: \n" + msg.Body);
            MessageBox.Show("Total Attachments: " + msg.Attachments.Count);
            for (int i = 1; i <= msg.Attachments.Count; i++)
            {
                MessageBox.Show("Attachment " + i + " :" + msg.Attachments[i].FileName);
                msg.Attachments[i].SaveAsFile("C:\\TestFileSave\\" + msg.Attachments[i].FileName);
            }

        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Error Occured In getting Mail info..: \n" + ex.ToString());
        }
    }

}

当我从我在Inbox文件夹中创建的文件夹中拖动邮件时,它会为每个被拖动的邮件提供相同的错误。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

错误是STG_E_FILENOTFOUND。

我不确定你的代码是如何工作的:你只是传递了Outlook用来命名它的文件名(它没有路径)(例如“My Subject.msg”)如果你拖了它消息到Windows资源管理器。您需要将内存流(上面代码中的文件流)保存到实际文件中。文件名没有任何区别。