[英]C# Outlook 2007 - How do I access attachment contents directly from my addin?
[英]How do I save attachment from Outlook using C#?
C# 新手並嘗試從 Python 轉換。 我用 Python 編寫了代碼,它可以瀏覽我的電子郵件並保存某些郵件的附件。 現在嘗試在 C# 中執行此操作,但遇到了一些問題。
我的第一個嘗試是添加引用,然后勾選 MS Outlook 16.0 對象庫,然后運行以下命令:
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace SaveEmail
{
internal class Program
{
private static void Main(string[] args)
{
try
{
Outlook.Application oApp = new Outlook.Application();
}
catch (Exception e)
{
Console.WriteLine($"Exception : {e}");
}
}
}
}
但即使是這個簡單的 init 也會失敗:
未處理的異常。 System.IO.FileNotFoundException: 無法加載文件或程序集 'office,版本 = 15.0.0.0,文化 = 中性,
我做了一些挖掘,似乎 Outlook 16.0 參考實際上指的是 OFFICE 2019。但在我們公司內部,我們運行的是 OFFICE 365。
鑒於我使用的是桌面版 Outlook (365),如何瀏覽我的郵箱並將附件保存在特定電子郵件中?
解決我的問題的兩個步驟:
首先,我必須創建一個新的 .NET FRAMEWORK 項目(而不是 CORE)
其次,下面的代碼用於讓我繼續:
Outlook.Application outlookApp = null;
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
// If not, create a new instance of Outlook and sign in to the default profile.
outlookApp = new Outlook.Application();
Outlook.NameSpace nameSpace = outlookApp.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);
nameSpace = null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.