Cache Mode Not allowing Sent Folder Update

G

Guest

--Outlook 2003 sp2
--VSTO 2005

My goal is to capture the e-mail in the Sent Items folder that was just sent
by the user. If Cache Mode is enabled, I never "get" the e-mail just sent, I
only get the last e-mail to enter the queue. If I remove Cache Mode, it works
like a champ, I get the e-mail just sent by the user. I have added delays,
but nothing works. Here's the code:

.......
Dim loopStartTime As Date = Date.Now
Dim numBeforeSent, numAfterSent As Integer

'objFolder is the Sent folder
numBeforeSent = objFolder.Items.Count

'_thisMail is MailItem
_thisMail.Send()

numAfterSent = objFolder.Items.Count

While Not numAfterSent > numBeforeSent
If DateDiff(DateInterval.Second, loopStartTime, Date.Now) > 30 Then
MsgBox("Unable to continue." & vbCrLf & "Reason: Unable to find the
email in the 'Sent Items' folder.", MsgBoxStyle.Critical)
Exit Function
Else
numAfterSent = objFolder.Items.Count
End If

End While

'''''''ONLY WORKS IN NON-CACHE MODE
sentItem.Item = objFolder.Items.GetFirst


How can I get the latest e-mail delivered to the Sent Items folder if I am
in Cache mode?
 
D

Dmitry Streblechenko

Use the Items.ItemAdd event on the Sent Items folder contents table rather
than wait for a newitem to appear in the folder.
Besides being extremely ineffcient, your code does not let the Windows
message pump run (which is used by the MAPI notification mechanism)

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
G

Guest

Dmitry, that worked perfectly as you have been telling me for the past few
weeks. It just took me a while to "get it". I converted your original C code
to VB.net using some help out there. My only issue is how to convert "Item"
to Redemption.SafeMailItem:

Public objFolder As Outlook.MAPIFolder
Public WithEvents objFolderItems As Outlook.Items

Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup

'Used to watch over the Sent Items list
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
objFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)

objFolderItems = objFolder.Items
AddHandler objFolderItems.ItemAdd, AddressOf objFolderItemAdded
End Sub

Public Sub objFolderItemAdded(ByVal Item As Object)
Public sentItem As Redemption.SafeMailItem

MsgBox("Item was added!!!")
sentItem = New Redemption.SafeMailItem

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This line fails
sentItem.Item = Item
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

SentItem.SaveAs........blah blah blah

End Sub

Thanks again Dmitry
 
D

Dmitry Streblechenko

Try something like the following:

Public Sub objFolderItemAdded(ByVal Item As Object)
Public sentItem As Redemption.SafeMailItem

MsgBox("Item was added!!!")
sentItem = New Redemption.SafeMailItem
sentItem.Item = Item
MsgBox sentItem.SenderName


Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
G

Guest

Yes, that works. What I have is a very interesting timing issue instead. What
happens is the e-mail is sent and my next step is to grab that Send Folder
safe e-mail and save it to the C:\.... drive. But I can't because the
objFolderItemAdded event does not occur fast enough. So what I am doing
instead is moving all steps into objFolderItemAdded which is kind of odd.
Does that makes sense? I just hate relying on the event function and moving
all control away from the main class I have written.
 
D

Dmitry Streblechenko

I don't see anything wrong here...
If you are concerned that the event mightt not fire for whatever reason, you
can save the message as a file before sending it, then overwrite the file
when the event fires. This way you will at least have something if ItemAdd
is not fired.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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