Starting a new Outlook 2007 Message from C# .NET

0

I'm looking for the most barebones complete example, to launch Outlook 2007 with a new message started via C# .NET (I happen to be using VS2008 .NET 3.0)

Here's what I'm trying:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;

namespace CreateMessage {

    class Program {

        static void Main ( string[] args ) {

            // Create outlook application object.
            var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();

            // Create mail message.
            var newMail = (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem( Microsoft.Office.Interop.Outlook.OlItemType.olMailItem );
            newMail.To = "example@exam.ple";
            newMail.Subject = "Example";
            newMail.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            newMail.HTMLBody = "<p>Dear Example,</p><p>Example example.</p>";
            newMail.Display( false );

        }

    }
}

Here are my project refereces:

alt text http://img4.imageshack.us/img4/9350/referencesi.jpg

Here's the intermittent exception I get (about half the time I run the program):

System.Runtime.InteropServices.COMException was unhandled
  Message="The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"
  Source="Interop.Microsoft.Office.Interop.Outlook"
  ErrorCode=-2147417846
  StackTrace:
       at Microsoft.Office.Interop.Outlook._MailItem.set_To(String To)
       at CreateMessage.Program.Main(String[] args) in C:\Users\Adam\Projects\GGS\CreateMessage\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Note: I've found many answers and examples about working with Outlook via Interop, but most of the answers don't show the using statements or are trying to do things at the next level up from what I'm shooting for. I'm just trying to get Outlook to pop open, with a new message started, and maybe a bit of info filled in (i.e. Subject, To, etc). Copying and pasting any of the examples I've found so far don't compile because they are snippets as opposed to a fully functioning barebones example that compiles.

thanks!

c#
.net
visual-studio-2008
interop
outlook
asked on Stack Overflow Sep 18, 2009 by Adam Kane • edited Sep 19, 2009 by Adam Kane

1 Answer

1

I'm not sure about your question, but the error message that you show indicates that you haven't added the assembly. To do this, expand your project, and right-click on References, and select 'Add Reference...'. Then go to the 'COM' tab (on VS2008 anyway) and locate "Microsoft Outlook 12.0 Object Library" (the specific version might be different on your machine depending on what version of Outlook is installed.) Highlight it and then click OK. I think that should clear your error, and will get you on the right path.

answered on Stack Overflow Sep 18, 2009 by Michael Bray

User contributions licensed under CC BY-SA 3.0