2016-08-05 68 views
1

我特林從我的C#(WinForm的)應用程序發送電子郵件至ADRESS前景,但我上線getteing錯誤如下:我試圖在C#中發送電子郵件了InvalidCastException錯誤

Outlook.MailItem mail =(Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem); 

我得到了以下錯誤:

Unable to cast COM object of type ‘Microsoft.Office.Interop.Outlook.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Outlook._Application’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{00063001-0000-0000-C000-000000000046}’ failed due to the following error: Library not registered. (Exception de HRESULT : 0x80040155)

我完整的代碼:

void SendEmailAuto() 
     { 
      try 
      { 
       OpenFileDialog attachment = new OpenFileDialog(); 
       attachment.Title = "Select a file to send"; 
       attachment.ShowDialog(); 
       Outlook._Application _app = new Outlook.Application(); 
       Outlook.MailItem mail =mail= (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem); 
       mail.To = '[email protected]"; 
       mail.Subject = "Text"; 
       mail.Body = "Funding Request Team"; 

       if (attachment.FileName.Length > 0) 
       { 
        mail.Attachments.Add(attachment.FileName, Outlook.OlAttachmentType.olByValue, 1, attachment.FileName); 
        mail.Importance = Outlook.OlImportance.olImportanceHigh; 
        ((Outlook.MailItem)mail).Send(); 
       } 
       else 
       { 
        MessageBox.Show("You are kindly requested to attach a document.", "MISSING FILE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       } 

      } 
      catch (Exception ex) 
      { 
       string msg = ex.Message; 
      } 
     } 
+0

你使用outlook發送電子郵件或網絡服務器?如果你是從服務器發送它,爲什麼不切斷前景中間人呢? – Takarii

+0

也許[這個問題](http://stackoverflow.com/questions/17810163/outlook-interop-exception-displaying-new-mailitem)可能有幫助嗎? –

+0

Takarii ..感謝您的回覆。我使用Outlook將郵件發送到另一個Outlook地址。從展望到展望 – Christ

回答