2017-02-17 65 views
0

我想用我的C#代碼創建outlook .msg格式文件。 我已經使用下面2代碼:在C#中創建outlook .msg文件

方法1:

Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application(); 

      // Creating a new Outlook message from the Outlook Application instance 
      Microsoft.Office.Interop.Outlook.MailItem msgInterop = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)); 

      // Set recipient information 
      msgInterop.To = "[email protected]"; 
      msgInterop.CC = "[email protected]"; 

      // Set the message subject 
      msgInterop.Subject = "Subject"; 

      // Set some HTML text in the HTML body 
      msgInterop.HTMLBody = "<h3>HTML Heading 3</h3> <u>This is underlined text</u>"; 

      // Save the MSG file in local disk 
      string strMsg = @"c:\\temp\TestInterop.msg"; 
      msgInterop.SaveAs(strMsg, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG); 

第二種方法:

Redemption.RDOSession Session = new RDOSession(); 
       Redemption.RDOMail Msg = Session.CreateMessageFromMsgFile(@"c:\temp\YourMsgFile.msg"); 
       Msg.Sent = true; 
       Msg.Subject = "test"; 
       Msg.Body = "test body"; 
       Msg.Recipients.AddEx("the user", "[email protected]", "SMTP", rdoMailRecipientType.olTo); 
       Msg.Save(); 

兩種方法給出了關於如下執行錯誤:

System.Runtime.InteropServices.COMException (0x8004010F): Creating an instance of the COM component with CLSID {29AB7A12-B531-450E-8F7A-EA94C2F3C05F} from the IClassFactory failed due to the following error: 8004010f Exception from HRESULT: 0x8004010F.

我已經研究並發現與平臺有一些兼容性問題。我試圖將平臺從32位更改爲x64。仍然沒有解決我的問題。

+0

我推薦使用.Net MailMessage內置功能將消息保存爲[此處介紹](http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as -eml-or-msg-file) – Esko

+0

MailMessage不允許創建我自己的示例電子郵件。當我嘗試設置爲To時,它表示它是隻讀變量。 – Neha

+0

你是什麼樣的電子郵件樣本?由於它是一個列表,所以屬性是隻讀的,你需要做message.To.Add(...) – Esko

回答

0

我在我的系統上安裝了Outlook。而我上面發佈的代碼(Microsoft.Office.Interop.Outlook.Application)就像一個魅力:)。

0

是否在機器上安裝了Outlook並處於可運行狀態?錯誤在於com組件沒有註冊,這通常意味着你只是從另一臺沒有註冊com的機器複製dll。

因此,無論安裝Outlook,或安裝此

https://www.microsoft.com/en-us/download/details.aspx?id=1004

+0

我安裝了它,沒有任何改變。拋出同樣的錯誤。 – Neha

+0

@Neha能否詳細介紹以下內容 - 對於運行的機器的每個操作系統,辦公室DLL以及VS版本設置的內容,64與x86的關係? –

0

兩種方法給你確切的同樣的錯誤?如果無法找到並加載MAPI系統,則贖回將返回0x8004010F(MAPI_E_NOT_FOUND)。

確保安裝了Outlook,並且您的應用的位數與Outlook的位數相匹配(請參閱http://www.dimastr.com/redemption/faq.htm#ErrorCreatingRedemptionObject)。如果無法安裝Outlook,則可以從https://www.microsoft.com/en-us/download/details.aspx?id=42040安裝獨立版本的MAPI,但請記住它不支持Unicode MSG文件。

相關問題