PDA

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



Steiner
07-13-2004, 04:58 AM
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:bug: ).

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 (:super: !!), 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? :help

Here's part of my code handling the sending:


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

jamescol
07-17-2004, 02:47 PM
Steiner,

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



'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)



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

Steiner
07-19-2004, 10:48 PM
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 :bore a collegue to test that, it may take a while.

jamescol
07-19-2004, 11:10 PM
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

Steiner
07-21-2004, 05:29 AM
Ok, I've just tested it, and it looks like it might work as planned :vv ! 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!:bigdance2

Thanks again for the tip using the FORCE_DOWNLOAD flag!!

Bye
Daniel

jamescol
07-21-2004, 05:37 AM
Daniel,
Great news! Glad we were able to help.

James