Email sent with Outlook Object Model stays in Outbox until I start Outlook -
i'm trying send emails .net application using outlook object model.
my application displays outlook message window user can see we're sending , edit first. when user hits send button, outlook window closes, , message gets sent. works long outlook application running.
if outlook application isn't running, message gets stuck in outbox, , not send until start outlook. when start outlook, can see message sitting in outbox folder few seconds, gets sent.
here simplified version of code i'm using send 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 running, nothing. if isn't, has // side-effect of initializing mapi use default profile , make object // model 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 test"; mailitem.htmlbody = "<html>a bunch of html</html>"; mailitem.display(true); note: if replace call mailitem.display() mailitem.send(), works whether outlook running or not. unfortunately that's not option, because need user able edit message before sending it.
i'm thinking need way detect when message has finished sending, , keep outlook.application object alive until then...but i'm not sure how that. app console application needs exit after sending email. putting thread sleep awhile doesn't (probably because whatever i'm waiting happening on same thread).
outlook exit if no more windows (explorers or inspectors) open or referenced.
reference inspector object, retrieve first syncobject object namespace.syncobjects , wait syncobject.syncend event fire.
insp = mailitem.getinspector; insp.display(true); //keep insp referenced
Comments
Post a Comment