Search code examples
vbaoutlook

How does the mailItem.sent parameter work?


I'm sending mails using Excel VBA:

With OutlookApplication.CreateItem(olMailItem)
    .Subject = topic
    .to = emailAddress
    .HTMLBody = content
    .Send
    msgbox .Sent
end with

where .Sent raises an error on my computer.

I asked why: How to keep reference to mail after sending in Outlook VBA?

I decided to ignore that error and assume that email reference disappears after sending.

Now a client reported that .Send returns False, when email was sent.

Why does .Sent lie?


Solution

  • The only allowed operation after you call MailItem.Send is to release your reference to the MailItem variable.

    Even if you could do that, Sent property would still be false: message submission is an asynchronous process. Outlook moves the message to the Outbox folder, and hands it over to the transport provider. Once it asynchronously sends the message, a new message is created in the Sent Items folder with all sender related properties populated. That (new) message will have the Sent property as true.