Link to home
Start Free TrialLog in
Avatar of Abacus IT
Abacus ITFlag for United States of America

asked on

Emailing through Outlook 2007 from VS2008 C# project.

I am trying to email out from my C# project an email to specific users that are stored in a table in a local SQL server table. I have added in the Microsoft.Office.Core, Microsoft.Office.Interop.Outlook, and Microsoft Office 12.0 Outlook references. I also added the using statements for the first 2 to my project code page, but when I attempt to build, I get an error statement saying:

Error      1      The type or namespace name 'Outlook' could not be found (are you missing a using directive or an assembly reference?)

I'm not sure what statement I am missing or reference I'm missing, but if I could get some assistance with this it would be much appreciated. I will attach the code I have so far for the emailing process. I have also tested the email using SMTP, which works fine, but the deployed project will not be able to use SMTP.

public void MainStart()
{
         string emailAddress = GetSystemSettings("emailAddress1");
         string emailSubject = GetSystemSettings("emailSubject");
         string emailMessage = GetSystemSettings("emailMessage");

         addToInBox(emailAddress, emailSubject, emailMessage);
}
public void AddToOutbox(string toValue, string subjectValue, string bodyValue)
{
       Outlook.Application oApp = null;
       Outlook.MailItem oMsg = null;
       Outlook.Inspector oAddSig = null;

       oApp = new Outlook.Application();
       oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
       oAddSig = oMsg.GetInspector;
       Outlook.MAPIFolder oOutboxFolder =       NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
       oNameSpace.Logon(null, null, false, false);

       Outlook._MailItem oMailItem =  (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
       oMailItem.To = emailAddress;
       oMailItem.Subject = emailSubject;
       oMailItem.Body = emailMessage;
       oMailItem.SaveSentMessageFolder = oOutboxFolder;
           
       //adds it to the outbox
       oMailItem.Send();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Abacus IT

ASKER

I like that solution much better than the route I was taking. Thank you.
Avatar of Comtek
Comtek

Can't you just send the email directly to your email server rather than using the client's Outlook? That would be a much cleaner setup.