c# - 有没有办法使用 C# 将 PST 文件导入 Outlook?

标签 c# outlook office-interop pst

使用:Visual Studio 2017(语言:C#)

我在下面用 PowerShell 脚本编写了一个类似的函数,但我需要它的 C# 版本才能在 Visual Studio 中单击按钮时执行:

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = new-object -comobject outlook.application

$namespace = $outlook.GetNameSpace("MAPI")

dir “$env:userprofile\Documents\Outlook Files\*.pst” | % { $namespace.AddStore($_.FullName) }

任何见解或代码示例将不胜感激。

最佳答案

您可以通过以下方式做到这一点:

在您的项目中,右键单击“引用”并添加对程序集“Microsoft.Office.Interop.Outlook”的引用。

然后你可以使用下面的代码:

/// <summary>
/// Get a reference to an already running or a newly started Outlook instance
/// </summary>
Microsoft.Office.Interop.Outlook.Application GetOutlookApp()
{
    Microsoft.Office.Interop.Outlook.Application app = null;

    // Try to get running instance
    try
    {
        app = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
    }
    catch(Exception)
    {
        // Ignore exception when Outlook is not running
    }

    // When outlook was not running, try to start it
    if(app == null)
    {
        app = new Microsoft.Office.Interop.Outlook.Application();
    }

    return app;
}

private void button1_Click(object sender, EventArgs e)
{
    const string fileName = @"D:\MyDings.pst";

    var app = GetOutlookApp();
    var nameSpace = app.GetNamespace("MAPI");

    nameSpace.AddStore(fileName);

    MessageBox.Show("Done");
}

关于c# - 有没有办法使用 C# 将 PST 文件导入 Outlook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559535/

相关文章:

c# - 从操作中获取方法名称

c# - 将 InputGestureText 显示为 Button 的工具提示

c# - 比较两个数据行

c# - 并行运行测试时使用 Console.SetOut 是否明智?

ssl - Outlook 和 Gmail 阻止 SSL 通讯中的图像

c# - Microsoft.Excel.Office.Core 和 Microsoft.Office.Interop.Excel 之间有什么区别?

c# - 获取仍在编辑的 Excel 单元格的值

c# - 从 .NET 中的 VBA 函数访问返回值?

html - 样式属性在 MS Outlook 中不起作用

python - 通过 python 发送时,附件不会显示在 Outlook 中