Error Sending email with Outlook
I'm trying to send email with Outlook. I've read the article "Sending Emails Through Outlook using C# and VB.NET" but when I try to get the Outlook.Application I get an error when I try to run it.
//Error
An unhandled exception of type 'System.InvalidCastException' occurred in OutlookTest.exe
Additional information: QueryInterface for interface Outlook._Application failed.
Here's what I do:
//globals
private Outlook.Application objApplication;
private Outlook.NameSpace objNameSpace;
//in a function
objApplication = new Outlook.Application();
objNameSpace = objApplication.GetNamespace("MAPI");
objNameSpace.Logon(null,null,true,true);
Outlook.MailItem objMailItem =
(Outlook.MailItem)objApplication.CreateItemOutlook.OlItemType.olMailItem);
objMailItem.To = "[email protected]";
objMailItem.Subject = "Test";
objMailItem.Body = "as dfkasjf a;sdfj ;alsdjf ;sadf";
objMailItem.Send();
I've also tried to do the same in VB but I get the same error.
I'm using Visual Studio .Net 2003 with the Microsoft Outlook 9.0 Library.
Does anyone have any idea of how to fix this?
nrajkumarPosted Mar 10, 2004, 11:51 PM