Can only send email via Outlook if Outlook is open

后端 未结 2 467
南笙
南笙 2020-12-03 08:23

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

相关标签:
2条回答
  • 2020-12-03 08:35

    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)

    0 讨论(0)
  • 2020-12-03 08:50

    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.

    0 讨论(0)
提交回复
热议问题