I got an InvalidCastException error while trying to send a Email in C#

1

I am tring to send a email from my C#(winform) app to an adress outlook but I am getteing an error on the line below :

Outlook.MailItem mail =(Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);

I got the following error :

Unable to cast COM object of type ‘Microsoft.Office.Interop.Outlook.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Outlook._Application’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{00063001-0000-0000-C000-000000000046}’ failed due to the following error: Library not registered. (Exception de HRESULT : 0x80040155)

My full code is :

void SendEmailAuto()
        {
            try
            {
                OpenFileDialog attachment = new OpenFileDialog();
                attachment.Title = "Select a file to send";
                attachment.ShowDialog();
                Outlook._Application _app = new Outlook.Application();
                Outlook.MailItem mail =mail= (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
                mail.To = 'paul.m@adt.org";
                mail.Subject = "Text";
                mail.Body = "Funding Request Team";

                if (attachment.FileName.Length > 0)
                {
                    mail.Attachments.Add(attachment.FileName, Outlook.OlAttachmentType.olByValue, 1, attachment.FileName);
                    mail.Importance = Outlook.OlImportance.olImportanceHigh;
                    ((Outlook.MailItem)mail).Send();
                }
                else
                {
                    MessageBox.Show("You are kindly requested to attach a document.", "MISSING FILE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }
c#
winforms
asked on Stack Overflow Aug 5, 2016 by Christ

1 Answer

0

Check this: http://www.fieldstonsoftware.com/support/support_gsyncit_2013.shtml , it might be some registry issue.

answered on Stack Overflow Aug 5, 2016 by Dexion

User contributions licensed under CC BY-SA 3.0