Search code examples
c#office-interopoutlook-redemption

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 = "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.


Solution

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