3

I am trying to create outlook .msg format file using my C# code. I have used below 2 code:

Method 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 = "neha1@gmail.com";
            msgInterop.CC = "neha@gmail.com";

            // 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);

Second method :

 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", "user@domain.demo", "SMTP", rdoMailRecipientType.olTo);
                Msg.Save();

Both method gives error on executing as below :

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.

I have researched and found some compatibility issue with platform. I tried to change the platform from 32-bit to x64. Still it did not resolve my problem.

Neha
  • 143
  • 4
  • 19
  • I recommend using .Net MailMessage built-in feature to save the message as [described here](http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file) – Esko Feb 17 '17 at 12:27
  • MailMessage does not allow to create my own sample email. When I tried to set To, it says it is read only variable. – Neha Feb 17 '17 at 13:01
  • what do you mean by sample email? To-property is ment to be readonly since it's a list, you need to do message.To.Add(...) – Esko Feb 17 '17 at 13:13

2 Answers2

1

Is outlook installed and in a runnable state on the machine you are doing this from? The error is that the com component isn't registered, which usually would mean you just copied dlls from another machine which didn't register the com ones.

So either install outlook, or install this

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

Eric Lizotte
  • 224
  • 1
  • 7
  • I installed it, nothing changed. Same error is thrown. – Neha Feb 17 '17 at 12:59
  • @Neha can you detail the following - 64 vs x86 for each of the OS of the machine its running on, the office DLLs and what the VS build is set to? – Eric Lizotte Feb 17 '17 at 13:15
0

I installed Outlook on my system. And the code I had posted above (Microsoft.Office.Interop.Outlook.Application) works like a charm :) .

Neha
  • 143
  • 4
  • 19