簡體   English   中英

創建Windows服務以通過Outlook Com互操作發送自動電子郵件

[英]Creating Windows service to send automated email through outlook com interop

我正在嘗試開發一個通過Outlook和Interop服務發送自動電子郵件的應用程序,並遇到以下異常。 下面是有例外的代碼。

 try
     {
       Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
      Microsoft.Office.Interop.Outlook.MailItem mailItem = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
     mailItem.Subject = "Details of Subject";
     mailItem.To = "example@gmail.com";
     mailItem.Body = "Automated email testing"
     // mailItem.Attachments.Add(logPath);//logPath is a string holding path to the log.txt file
     mailItem.Importance = Outlook.OlImportance.olImportanceHigh;
     mailItem.Display(false);
     mailItem.Send();
 }
catch (Exception ex)
  {
         WriteErrorLog("\n");
         WriteErrorLog(ex.Message);
         WriteErrorLog(ex.StackTrace);
}

並拋出一個異常:

System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj) 
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at WServiceMixInOne.ConnectionLogs.MainClass() in C:\Users\admin\documents\visual studio 2017\Projects\WServiceMixInOne\WServiceMixInOne\ConnectionLogs.cs:line 120

第120行是第一行應用程序= new Microsoft.Office.Interop.Outlook.Application();

嘗試從后台進程關閉Outlook。 然后執行您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM