在C#中创建outlook .msg文件
我想用我的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。仍然没有解决我的问题。
我在我的系统上安装了Outlook。而我上面发布的代码(Microsoft.Office.Interop.Outlook.Application)就像一个魅力:)。
是否在机器上安装了Outlook并处于可运行状态?错误在于com组件没有注册,这通常意味着你只是从另一台没有注册com的机器复制dll。
因此,无论安装Outlook,或安装此
https://www.microsoft.com/en-us/download/details.aspx?id=1004
我安装了它,没有任何改变。抛出同样的错误。 – Neha
@Neha能否详细介绍以下内容 - 对于运行的机器的每个操作系统,办公室DLL以及VS版本设置的内容,64与x86的关系? –
两种方法给你确切的同样的错误?如果无法找到并加载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文件。
我推荐使用.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