【问题标题】:Error: Cannot open xxx.msg in Outlook Drag and Drop add in错误:无法在 Outlook 拖放添加中打开 xxx.msg
【发布时间】:2013-11-16 05:00:57
【问题描述】:

我正在为 Outlook 创建加载项。我在其中创建了拖放用户控件。
当我拖放邮件时,会提取一些邮件并提供适当的信息,但在收件箱中的某些邮件中,会出现类似错误:

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

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());
        }
    }

}

当我从我在收件箱文件夹中创建的文件夹中拖动邮件时,对于每个被拖动的邮件,都会出现相同的错误。

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    错误是 STG_E_FILENOTFOUND。

    我不确定您的代码是如何工作的:您传递的只是文件名(它没有路径),如果您将消息到 Windows 资源管理器。您需要将内存流(上面代码中的文件流)保存到实际文件中。文件名没有任何区别。

    【讨论】:

    • 嘿,如果你想你可以试试那个代码。我可以使用上面的代码获取邮件信息。但在某些邮件中,它给了我错误。我得到邮件信息的原因是文件名对象包含 .msg 文件名,而另一个对象包含收件箱文件夹信息。这样我就可以通过拖放获取邮件信息。
    • 调用 CreateItemFromTemplate 时文件名变量的确切值是多少?
    • 这是 .msg 文件的名称。例如“我的邮件文件.msg”
    • 那么 CreateItemFromTemplate 怎么可能打开它,除非您在当前目录或 PATH 中的某个位置有一个具有该文件名的 MSG 文件?将流保存到文件并将完全限定的文件名传递给 CreateItemFromTemplate。
    • 如果您立即重新打开邮件,为什么需要先保存它?要从 Outlook 中拖动消息,您需要先选择它,这意味着 Application.ActiveExplorer.Selection 集合将具有被拖动的消息。
    猜你喜欢
    • 2016-06-18
    • 2012-06-02
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多