我想用我的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。仍然沒有解決我的問題。
我推薦使用.Net MailMessage內置功能將消息保存爲[此處介紹](http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as -eml-or-msg-file) – Esko
MailMessage不允許創建我自己的示例電子郵件。當我嘗試設置爲To時,它表示它是隻讀變量。 – Neha
你是什麼樣的電子郵件樣本?由於它是一個列表,所以屬性是隻讀的,你需要做message.To.Add(...) – Esko