Messages in Outbox with Sent time of None

M

Michael

I have written an Outlook add-in in C#.Net and may be experiencing a problem
I've seen mentioned in some Outlook newsgroups.

One of my users is reporting that she has messages that sometimes stay stuck
in the Outlook and they have a Sent time of None. I have seen mention of
problems like this when the user browses to the Outbox, though this is not
happening in this case (see http://www.slipstick.com/problems/outbox.htm).

Several people commented that the problem occurs when an add-in is written
in .Net. However, no one has described what a .Net add-in must do, or not
do, to cause/fix this problem.

My add-in does examine messages in the ItemSend event and acts on them if
the subject line contains certain text. This problem is occurring on
messages that do not have this text, so the add-in is doing little more than
getting the mailitem object, reading the subject, and releasing the
mailitem.

Does anyone have any idea how to avoid this problem?

Michael
 
D

Dave Kane [MVP - Outlook]

One of the issues with .NET add-ins is that the garbage collector determines
when references are actually released. So unless you are explicitly calling
Marshall.ReleaseComObject and then forcing garbage collection your reference
may not be released as far as Outlook is concerned. Garbage collection is
expensive so you need to be judicious about forcing it, and if you need to
call Marshall.ReleaseComObject you should be using a shim so that your
add-in is running in it's own AppDomain to prevent it clobbering the COM
object for other add-ins (if you are not familiar with shims here's a good
intro with lots of links
http://www.shahine.com/omar/PermaLink,guid,3d8a0445-379a-4e0b-bc7c-697a17060040.aspx)

It's curious that you are only getting reports of this from one user and
that it's sporadic. What's different about her environment - Outlook
version, send settings?
 
M

Michael

Upon further testing, other users have reported this problem. I have asked
the original user to upgrade to Service Pack 2 of Office/Outlook to see if
that helps, but it seems unlikely.

I am using a shim and I'm using Marshall.ReleaseComObject as well, though I
am not forcing garbage collection in this event (only when exiting Outlook).
Do you think I'd need to force garbage collection (and
WaitForPendingFinalizers) in this event handler (ItemSend) in order to
prevent this problem?

Michael
 
J

Jevon

I had a similar problem when catching the Item_Send event in VBA. Even if I
only read properties of the item (didn't change anything), they got put in
the Outbox with None. I "fixed" the problem by calling Item.Save at the end
of the event - I having a feeling this is more of a hack than a fix, hence
the quotes, but you might find it helps your problem and provide a direction
for further investigation? Please follow up with any further info you find
if so!

Jevon
 
J

Jevon

Hmm.... It turns out I've commented out the Item.Save line at some point and
it's still working, so this might be a red-herring. Sorry!

Jevon
 
M

Michael

Could this be related to Outlook being Offline at times? The user who has
experienced this most often had two profiles, and I noticed that one had
become Offline at some point.

No thoughts on whether or not I should be doing garbage collection at the
end of my ItemSend event handler?

Michael
 
D

Dave Kane [MVP - Outlook]

Looking back at your original post, you mention that only the items that you
examine but don't change are showing this problem. What are you doing with
the items that you change - do you call a Send again?
 
M

Michael

Dave,

In the Application.ItemSend event, I check to see if it's a MailItem and
look at the subject line for some specific text. If it has my text, I make
sure there are some recipients by saving the MailItem and using a
Redemption.SafeMailItem to resolve the recipients and check the count. If
that's all good, I modify the subject and plant some text in the Mileage
field (which I examine and remove when it gets to the Sent Items folder),
and save the MailItem.

Now, if the user got here by clicking the Outlook's Send button, I don't do
anything else. If they got here by clicking my special send button (which
does some non-Outlook stuff prior to all this), I do a MailItem.Close
(discarding changes) and a MailItem.Send(). Users are mostly clicking
Outlook's Send button (and sending messages without my specific text), so I
know the problem happens then. I can't confirm if it happens when they
press my button.

Michael
 
D

Dave Kane [MVP - Outlook]

From your description the problem is happening with items where you look at
the subject and do nothing else, which sounds pretty odd. Can you post your
code?
 
M

Michael

Below is my C# code from the ItemSend event, with a little irrelevant code
removed from the middle. Keep in mind that the problem occurs even when the
inner if statement expression evaluates to false.

private void Application_ItemSend(object Item, ref bool Cancel)
{
if (_showSendWarning)
{
Outlook.MailItem mailItem = Item as Outlook.MailItem;

try
{
if (mailItem != null)
{
string subject = (mailItem.Subject == null ? "" : mailItem.Subject);
string mileage = mailItem.Mileage;

if (CommonEmail.EmailText.GetEmailTagIndex(subject) > 0 &&
(mileage == null || mileage.Length == 0 ||
!mileage.StartsWith(EmailInspectorStandardToolbar._buttonSendAndFileInContextStr)))
{
..
..
..
}
}
}
catch (System.Exception e)
{
Log.Write(this, TraceLevel.Error, "Problem in ItemSend event
handler.");
Log.Write(this, TraceLevel.Error, e);
}

ComObjects.ReleaseObject(mailItem);
mailItem = null;
}

ComObjects.ReleaseObject(Item);
Item = null;
}

Note: ComObjects.ReleaseObject does a Marshal.ReleaseComObject(o);

Michael
 
M

Michael

I'm starting to think this is not related to the ItemSend event. While
messages initiated from an Outlook send/reply have gotten stuck, my users
think that there is also a message sent from Outlook automation in our
standalone application also stuck in the Outbox. We're not sure, because
this doesn't happen very often, but we've seen them in pairs. I thought it
was worth posting here to see if there is another cause to the problem.

The C# application gets the Outlook application object, creates a MailItem,
preserves it in memory, and sends it. Around the time this problem began, I
made a change to my "inspector wrapper". Previously, the app would hold
onto the preserved COM objects until it exited, which often caused Outlook
to stay running. The change was to release all the COM objects (e.g. the
MailItem) when an inspector is closed and Explorers.Count == 0 and
Inspectors.Count <= 1, which is what other inspector wrappers do.

When I release the COM objects as described, Outlook tends to exit prior to
sending the message, even if we check the "Perform and automatic
send/receive with exiting." checkbox in the Send/Receive settings in
Outlook. The next time you get into Outlook, you see the message in the
Outbox. We think that sometimes it's sent on Outlooks initial send/receive,
sometimes on the second one (or a manual send/receive), and sometimes never
(that's the problem). In fact, the same behavior seems to occur without our
software when you do a Send To --> Mail Recipient from Windows Explorer when
Outlook is not running. Though none got stuck in the Outbox, we don't do
that very often.

When we were not releasing the COM objects right away (like we should), the
side effect was that Outlook would stay around long enough to send the
message. So, could this be related to Outlook exiting around the time it
wants to send the message?

Michael
 
D

Dave Kane [MVP - Outlook]

I would eliminate these statements:
ComObjects.ReleaseObject(Item);
Item = null;
They may be irrelevant but they aren't necessary either.

Also, I'd suggest wrapping Outlook.MailItem mailItem = Item as
Outlook.MailItem; within a try..catch. Not every Item returned by ItemSend
is going to be a MailItem so the Cast will sometimes fail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top