Can only send email via Outlook if Outlook is open

后端 未结 2 488
南笙
南笙 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: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.

提交回复
热议问题