Can only send email via Outlook if Outlook is open

19,267

Solution 1

The following code has reliably worked for months for me:

            app = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI");
            f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Thread.Sleep(5000); // a bit of startup grace time.

if outlook was open it uses it, if not its opened it. Of course, if your outlook requires you to login, your code wont allow for that. Some systems make it difficult for you to auto login.

Solution 2

I didn't like the idea to use Thread.Sleep for 5 seconds, so I've found another solution, that worked for me:

All you need is get Inspector object for newly created MailItem

Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMsg.GetInspector;

Answer was published in Google groups originally for Outlook 2007 (but it worked for me with Outlook 2010)

Share:
19,267

Related videos on Youtube

Kasper Hansen
Author by

Kasper Hansen

Updated on September 16, 2022

Comments

  • Kasper Hansen
    Kasper Hansen over 1 year

    I want to use send emails via Outlook as described here. It works fine as long as I have already opened Outlook. So for example if Outlook is minimized and I execute my code, then I can send an email just fine. But if Outlook is closed, then I get an exception:

    {System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
       at Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()
       at OutlookExample.Form1.btnSendEmail_Click(Object sender, EventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\OutlookExample\OutlookExample\Form1.cs:line 28}
    

    Here is the code:

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    ...
    
    private void btnSendEmail_Click(object sender, EventArgs e)
    {
        try
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                oMsg.HTMLBody = "Hello, here is your message!";
                oMsg.Subject = "This is a test message";
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
                oRecip.Resolve();
                oMsg.Send();
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    

    Why doesn't this work?

    Edit: Here is the solution

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    ...
    
    private void btnSendEmail_Click(object sender, EventArgs e)
    {
        try
        {
            Outlook.Application oApp = new Outlook.Application();
    
            // These 3 lines solved the problem
            Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
            Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            System.Threading.Thread.Sleep(5000); // test
    
            Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                oMsg.HTMLBody = "Hello, here is your message!";
                oMsg.Subject = "This is a test message";
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
                oRecip.Resolve();
                oMsg.Send();
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    
    • SLaks
      SLaks almost 12 years
      Don't use Outlook. Instead, use System.Net.Mail.
  • B.K.
    B.K. about 9 years
    How does that inspector object help, though? It's not being used anywhere or am I missing something?
  • Woodman
    Woodman about 9 years
    It appeared that Outlook returns Inspector only after it gets initialized properly. That's the trick. You don't have to use it .
  • B.K.
    B.K. about 9 years
    So, do you check to see if it's null? Or does it simply force Outlook to initialize if you're trying to get the inspector object and Outlook hasn't been started?
  • Woodman
    Woodman about 9 years
    From what I remember it simply forces Outlook to initialize. And I'm not checking it's value,