与Outlook对象模型一起发送的电子邮件将保留在“发件箱"中,直到我启动Outlook [英] Email sent with Outlook Object Model stays in Outbox until I start Outlook

查看:132
本文介绍了与Outlook对象模型一起发送的电子邮件将保留在“发件箱"中,直到我启动Outlook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Outlook对象模型从.NET应用程序发送电子邮件.

I'm trying to send emails from a .NET application using Outlook Object Model.

我的应用程序显示Outlook消息窗口,以便用户可以看到我们正在发送的内容并首先对其进行编辑.当用户单击发送"按钮时,Outlook窗口将关闭,并且邮件将被发送.只要Outlook应用程序已在运行,它就可以完美运行.

My application displays the Outlook message window so the user can see what we're sending and edit it first. When the user hits the Send button, the Outlook window closes, and the message gets sent. This works perfectly as long as the Outlook application is already running.

如果Outlook应用程序尚未运行,则该消息将卡在发件箱中,并且直到我启动Outlook才会发送.启动Outlook时,我可以看到邮件在发件箱文件夹中停留了几秒钟,然后被发送.

If the Outlook application isn't already running, the message gets stuck in the Outbox, and will not send until I start Outlook. When I start Outlook, I can see the message sitting in the Outbox folder for a few seconds, then it gets sent.

这是我用来发送电子邮件的代码的简化版本:

Here is a simplified version of the code I'm using to send the email:

Outlook.Application app = new Outlook.Application();
var ns = app.GetNamespace("MAPI");

// (ref: https://msdn.microsoft.com/en-us/library/office/ff861594.aspx)
// If outlook is already running, this does nothing.  If it isn't, this has the 
// side-effect of initializing MAPI to use the default profile and make the object 
// model fully functional
var mailFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

var mailItem = (Outlook.MailItem)ns.Application.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.To = "me@nowhere.com";
mailItem.Subject = "This is a test";
mailItem.HTMLBody = "<html>A bunch of HTML</html>";

mailItem.Display(true);

注意:如果我用mailItem.Send()替换对mailItem.Display()的调用,则无论Outlook是否运行都可以使用.不幸的是,这不是一个选择,因为我需要用户能够在发送消息之前先对其进行编辑.

NOTE: If I replace the call to mailItem.Display() with mailItem.Send(), it works whether Outlook is running or not. Unfortunately that's not an option, because I need the user to be able to edit the message before sending it.

我在想我需要一种方法来检测消息何时完成发送,并使Outlook.Application对象保持活动状态,直到那时...但是我不确定如何做到这一点.我的应用程序是一个控制台应用程序,在发送电子邮件后需要退出.将线程置于睡眠状态不会这样做(可能是因为我正在等待的东西在同一线程上发生).

I'm thinking I need a way to detect when the message has finished sending, and keep the Outlook.Application object alive until then...but I'm not sure how to do that. My app is a console application that needs to exit after sending the email. Putting the thread to sleep for awhile doesn't do it (probably because whatever I'm waiting for is happening on that same thread).

推荐答案

如果没有其他窗口(浏览器或检查器)打开或引用,Outlook将退出.

Outlook will exit if no more windows (explorers or inspectors) are open or referenced.

引用检查器对象,从Namespace.SyncObjects中检索第一个SyncObject对象,并等待SyncObject.SyncEnd事件触发.

Reference an inspector object, retrieve the first SyncObject object from Namespace.SyncObjects and wait for the SyncObject.SyncEnd event to fire.

insp = mailItem.GetInspector;
insp.Display(true);
//keep insp referenced

这篇关于与Outlook对象模型一起发送的电子邮件将保留在“发件箱"中,直到我启动Outlook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆