Neha
Neha

Reputation: 143

Create outlook .msg file in C#

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 = "[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);

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", "[email protected]", "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.

Upvotes: 3

Views: 10229

Answers (2)

Neha
Neha

Reputation: 143

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

Upvotes: 0

Eric Lizotte
Eric Lizotte

Reputation: 224

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

Upvotes: 1

Related Questions