Consulting

Results 1 to 6 of 6

Thread: Solved: MAPI: Ensure Mail is really sent and not just in the Outbox

  1. #1
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location

    Solved: MAPI: Ensure Mail is really sent and not just in the Outbox

    Hello all,

    I just have a problem automating Outlook (as if you could not have guessed that ). I've set up an Excel Workbook which will extract some data and then send it using MAPISendMail (can't use Outlook Objects because some users still have MSMail running ).

    The basic problem is, that the sent mails land in the Outbox, but since my programs logs out from MAPI right after creating the mail, the mail lies there until the mail client is started next time (which can be some days later, best was 3 months ( !!), some people just don't like eMail at all).

    Is there a way to force all messages to be sent before allowing my program to log out?

    Here's part of my code handling the sending:

    [VBA]
    Dim mRecip(0) As MapiRecip, mFile(0) As MapiFile, mMail As MAPIMessage, lSess&, lRet&

    [...snip Just setting up the mail, attachments etc. here...]

    lRet = MAPILogon(0, "", "", MAPI_LOGON_UI, 0, lSess)
    If lRet <> SUCCESS_SUCCESS Then
    strError = "Fehler beim Einloggen in Mailsoftware. (" & CStr(lRet) & ")"
    GoTo ErrorHandler
    End If

    lRet = MAPISendMail(lSess, 0, mMail, mRecip, mFile, MAPI_NODIALOG, 0)
    If lRet <> SUCCESS_SUCCESS And lRet <> MAPI_USER_ABORT Then
    If lRet = 14 Then
    strError = "Zieladresse kann nicht gefunden werden, stellen Sie sicher, dass das globale Adressbuch zur Verf?gung steht und die Adresse g?ltig ist."
    Else
    strError = "Fehler beim Senden: " & CStr(lRet)
    End If
    GoTo ErrorHandler
    End If

    lRet = MAPILogoff(lSess, 0, 0, 0)
    SendIt = True
    Exit Function
    ErrorHandler:
    If strError = "" Then strError = Err.Description
    Call MsgBox(strError, vbExclamation, "Fehler beim Senden")
    End Function
    [/VBA]

  2. #2
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Steiner,

    To send the mail immediately, you could add another modified MAPILogon after your MAPILogoff.

    [vba]

    'your existing code
    '.
    '.
    lRet = MAPILogoff(lSess, 0, 0, 0)

    'Then logon again using the MAPI_FORCE_DOWNLOAD flag
    MAPILogon(0, "", "", MAPI_LOGON_UI + MAPI_FORCE_DOWNLOAD, 0, lSess)
    lRet = MAPILogoff(lSess, 0, 0, 0)
    [/vba]


    Setting the MAPI_FORCE_DOWNLOAD bit forces a send/receive of messages to/from the server.

    Too bad this flag isn't available in the MAPILogoff function

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  3. #3
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Thank you for the input James , I'll give it a try!

    You're right, it's a shame that this function is not available with Logoff. I don't really like a 2nd logon, because that means that people will have to enter their username and password twice... Maybe if I leave out the first Logoff?
    I'll try that, but since I have to bother a collegue to test that, it may take a while.

  4. #4
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    I suppose you could perform another logon before the initial logoff. There really isn't anything preventing you from sharing the same MAPI session. Never tried it with this purpose in mind.

    You could also just change your initial logon. Then email would be delivered on an N+1 schedule. If users receive mail frequently enough via your program, the delay might not be noticeable.

    The only other way I could think of was to somehow write some automation code to activate the MSMAIL Send/Receive button. I can't dig up any information to know if that's even possible.

    Let us know how the test works out.

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  5. #5
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location

    Talking Success

    Ok, I've just tested it, and it looks like it might work as planned ! The real test with all users will happen in 3 months earliest, but what I've seen so far looks promising!

    I just created a 2nd MAPI-session before the first one is finished. That way, the user does not need to enter username and password again, since there is already an open connection . The 2nd session logs in with forced download, so I can see the download window for a millisecond, and the mail is really sent not just sitting in the outbox.
    Now I close first the 2nd session, then the original one and everybody is happy again!

    Thanks again for the tip using the FORCE_DOWNLOAD flag!!

    Bye
    Daniel

  6. #6
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Daniel,
    Great news! Glad we were able to help.

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •