javavbaemailoutlookjacob

Wait while Mail is not sent/closed with jacob


My application sends Emails with jacob. Now I want to just open the Mail in some cases and wait for the user to press send (or he closes the mail)

ActiveXComponent axcOutlook = new ActiveXComponent("Outlook.Application");
Dispatch mail = Dispatch.invoke(axcOutlook.getObject(), "CreateItem", Dispatch.Get, new Object[] { "0" }, new int[0]).toDispatch();
...
Dispatch.put(mail, "Subject", subject);
Dispatch.put(mail, "Body", sbBody.toString());
Dispatch.put(mail, "ReadReceiptRequested", "false");
Dispatch.call(mail, "Display");
//And here I want to wait till the Mail is sent/closed

I've tried it with an while(true) loop

while (true) {
    if (Dispatch.get(mail, "Sent").getBoolean()) {
        return;
    }
}

But with this approch i get an exception (after I've sent the mail):

com.jacob.com.ComFailException: Invoke of: Sent

Source: Microsoft Outlook

Description: the element was moved or deleted.


Solution

  • You need to handle the Send event of the MailItem class which is fired when the user selects the Send action for an item.

    Also you may find the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. Note, if the event procedure sets the Cancel argument to true, the send action is not completed and the inspector is left open.