none
Cannot LOGON when creating a new instance of Outlook RRS feed

  • Question

  • Exchange Server 2010 environment.

    When Outlook is already open, I have no problem getting a handle on the instance, and then sending an email programmatically. However, if I need to do a CreateObject and open a new instance, even though I attempt a Namespace Logon with the default profile, the email fails to send.

    Not bAlreadyRunning = New instance was created
    Not Application.UserControl = user is not present (being run from the scheduler)

    If Not bAlreadyRunning And Not Application.UserControl Then
        oNS.Logon cProfileDefault, , False, False  ' default profile,no password,don't prompt, no new session
    End If

    Is this a security issue and I am forced to enter the password ?
    This sounds crazy but is there an API call to retrieve the password for the user that's already logged-in ?

    Wednesday, October 2, 2013 2:41 PM

Answers

  • Message submission is asynchronous, plus Outlook will not perform Send/Receive immediately.

    Call Namespace.SendAndReceive or use Namespace.SyncObjects colelciton to call SyncObjct.Start on a particular Send/Receive group.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 3:26 PM
  • No, you need to call MailItem.Send, then SyncObject.Start.

    When SyncObject.SyncEnd event fires, you are all done and you can release all Outlook objects.

    Note that since the out-of-proc COM system uses Windows messages to communicate, your app must *not* call sleep() and must pump Windows messages after sending the message. Otherwise SyncEnd event will never fire. SyncObject object must be alive - if it is released or garbage collected, the event will nto fire.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 4:59 PM
  • No, you call Send first.

    When you call Send, Outlook moves the message to the Outbox folder and marks it as ready-for-submission (that is why it is shown italicized).

    Then you call SyncObject.Start. Outlook will take that as a hint that it must process incoming and outgoing queues now rather than later when the next send/receive interval timer fires.

    If you are using a closely coupled store/transport provider (such as the online Exchange store), SyncObject.Start is not required since the store (Exchange) will be sending the message rather than some other transport provider (such as SMTP).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 5:54 PM
  • And finally, FINALLY it is working perfectly !!!

    I created a small class, grabbed the SyncEnd event handler and that did the trick.

    Thanks Dmitry, you're the best.

    I wonder what would happen if this were not an exchange server installation and instead, just a PST file set-up connected to verizon or another isp via SMTP ? Would the SyncObject no longer exist ?

    Wednesday, October 9, 2013 7:19 PM

All replies

  • Message submission is asynchronous, plus Outlook will not perform Send/Receive immediately.

    Call Namespace.SendAndReceive or use Namespace.SyncObjects colelciton to call SyncObjct.Start on a particular Send/Receive group.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 3:26 PM
  • Thanks much Dmitry. Would it help if I place a call to the Sleep API function for say 2000 milliseconds before the send ?
    Wednesday, October 2, 2013 3:31 PM
  • What if it takes longer than that? If you are using SyncObject object, you can use SyncStart/SyncEnd events to figure out when the message submission is done.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 3:34 PM
  • Ah...so there is a callback event then ?

    So I need to instantiate a Synchobject so that when the send/receive is ready, I can then send the message, right ?

    Public WithEvents moSyncObj as Outlook.SynchObject

    Set moSyncObj = oNS.SynchObjects(0)

    Public Sub moSyncObj_End()

        oMsg.Send ' send after we are synched-up

    End Sub

    Wednesday, October 2, 2013 4:51 PM
  • No, you need to call MailItem.Send, then SyncObject.Start.

    When SyncObject.SyncEnd event fires, you are all done and you can release all Outlook objects.

    Note that since the out-of-proc COM system uses Windows messages to communicate, your app must *not* call sleep() and must pump Windows messages after sending the message. Otherwise SyncEnd event will never fire. SyncObject object must be alive - if it is released or garbage collected, the event will nto fire.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 4:59 PM
  • No, you need to call MailItem.Send, then SyncObject.Start.

    When SyncObject.SyncEnd event fires, you are all done and you can release all Outlook objects.

    Note that since the out-of-proc COM system uses Windows messages to communicate, your app must *not* call sleep() and must pump Windows messages after sending the message. Otherwise SyncEnd event will never fire. SyncObject object must be alive - if it is released or garbage collected, the event will nto fire.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    No, you mean I must place the send in the SynchObject's Start event handler, right ?
    It doesn't look right that I send and then issue the Start method.....sounds reversed.

    oNS.SynchObjects(0).Start ' this should first the start event handler

    Public Sub moSyncObj_Start()

      oMailItem.Send

    End Sub

    Wednesday, October 2, 2013 5:34 PM
  • No, you call Send first.

    When you call Send, Outlook moves the message to the Outbox folder and marks it as ready-for-submission (that is why it is shown italicized).

    Then you call SyncObject.Start. Outlook will take that as a hint that it must process incoming and outgoing queues now rather than later when the next send/receive interval timer fires.

    If you are using a closely coupled store/transport provider (such as the online Exchange store), SyncObject.Start is not required since the store (Exchange) will be sending the message rather than some other transport provider (such as SMTP).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 2, 2013 5:54 PM
  • Thanks so much for your help Dmitry.
    I'll give it a shot tomorrow.

    Wednesday, October 2, 2013 8:59 PM
  • Strangely, now I'm getting a 429 error "ActiveX component can't create object" at the .Send invocation which is right before the .Start call.

    It appears I failed to create a new MailItem....or something.

    Tuesday, October 8, 2013 2:05 PM
  • Strangely, now I'm getting a 429 error "ActiveX component can't create object" at the .Send invocation which is right before the .Start call.

    It appears I failed to create a new MailItem....or something.


    My bad....forgot to clear the Err object. Now "good to go"...all is well and working....thanks to Dmitry.
    Wednesday, October 9, 2013 1:07 PM
  • Nope, still does not work unless Outlook is already open. Emails just hang in the Outbox.

    The problem appears to be the ExchangeConnectionMode is set to 700 (cached)....and it is read-only.

    I believe it needs to be set to olOnline, but how to do that ?

    Wednesday, October 9, 2013 3:36 PM
  • What is your latest code that uses the SyncObjects collection?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 9, 2013 5:04 PM
  • Set oNS = oOutlook.GetNamespace("MAPI")
      If Not bAlreadyRunning Then
        oNS.Logon cProfileDefault, , False, False  ' default profile,no password,don't prompt, no new session
      End If
      Set oMailFolder = oNS.GetDefaultFolder(olFolderInbox)
      Set oOutMail = oOutlook.CreateItem(0)
        ' remove trailing delimiter
        .To = StringTruncateBy(.To, 1)
        .CC = StringTruncateBy(.CC, 1)
        .BCC = StringTruncateBy(.BCC, 1)
        .Subject = pszSubject
        .BodyFormat = pBodyFormat
        szBody = pszBody
        If pBodyFormat = OlBodyFormat.olFormatHTML Then
          If Not Left(szBody, 6) Like "<html>" Then
            szBody = "<html>" & szBody & "</html>"
          End If
          .HTMLBody = szBody
        Else
          .Body = szBody
        End If
        Set oSynch = oNS.SyncObjects(1)
        
        .Send
        oSynch.start
     

    I've researched this and it appears to be related to the ConnectionMode of Exchange.

    However, now I wonder if my last Logon parameter should be set to True (new session) ?

    Wednesday, October 9, 2013 5:10 PM
  • You need to wait for the oSynch.SyncEnd event to fire. Otherwise you just release all variables and Outlook shuts down.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 9, 2013 5:17 PM
  • Ah....I wondered if I had to trap that event....thanks for that.

    ' at module level

    Public WithEvents oSynch as Outlook.SyncObject

    Private mbSyncCompleted as Boolean

    oSynch.Start

    Do Until mbSyncCompleted

       DoEvents

    Loop

    oNS.LogOff

    Set oNS = Nothing

    Public Sub oSynch_SyncEnd()

       mbSyncCompleted = True

    End Sub

    Wednesday, October 9, 2013 6:32 PM
  • And finally, FINALLY it is working perfectly !!!

    I created a small class, grabbed the SyncEnd event handler and that did the trick.

    Thanks Dmitry, you're the best.

    I wonder what would happen if this were not an exchange server installation and instead, just a PST file set-up connected to verizon or another isp via SMTP ? Would the SyncObject no longer exist ?

    Wednesday, October 9, 2013 7:19 PM