0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
griFlo
  • 2,084
  • 18
  • 28

1 Answers1

1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Nice. The thing with the `Send`event is working. But I'm not able to verify if the Mail is closed. I do get the event `close`, but it appears before the, `do you want to save ...`. If you choose cancel in outlook, the event is fired even if the Mail is still open. do you have any idea how to solve this? – griFlo Jul 21 '15 at 06:04
  • ok. I've not found a answer to my close-problem. So before i waste more time for this, I just close the Item without manually if the close event is fired. – griFlo Jul 21 '15 at 09:58