none
vb.net large emails behaving unexpectedly vb RRS feed

  • Question

  • Me again.

    Have an outlook vb addin that processes hundreds of emails in a day, all successfully handling and moving to one of two outlook folders.

    I have however noticed that for some reason, the odd email remains in the inbox and doesnt get moved, which is really the last step in the program.

    In an attempt to resolve, as the emails I process are unread, I now mark each email as unread = false, save and in this instance delete, effectively moving these into the Deleted folder.

    Appears to have limited success, but still get the odd email, whilst now moved, still appears as "unread".

    How can this be when it's literally 3 lines of code?

    The only thing I can think of is there's something odd re emails with large attachments?

    Any thoughts?

    Regards and as always, thanks in advance

    Perry

    Friday, August 8, 2014 6:55 PM

All replies

  • And what those "literally 3 lines of code" are?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, August 8, 2014 7:02 PM
  • Here you go

                                mail.UnRead = False
                                mail.Save()
                                mail.Delete()

    Yet, alatest example, email 9MB was moved to Deleted folder but reamiained "unread"

    Friday, August 8, 2014 7:14 PM
  • Hello Perry, Did you try to debug the code? Do you get any exceptions or error messages in the code? What code are you talking about? Could you please be more specific? BTW Do you use the IMAP protocol?
    Friday, August 8, 2014 7:15 PM
  • How is mail object retrieved? Are you looping through a collection of items?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, August 8, 2014 7:30 PM
  • Debug, no not as yet, but neither does the program error.

    Do I need trap for errors, even when none appear to be reported?

    The code, or statements are simply the 3 provided earlier. How can a single unread = false not work, even when followed by the mail.save()

    Regarding IMAP protocol, the code is straight forward VB, VB.NET in OutlookNameSpace (connected to Exchange).  No POP etc. Does that help

    Cheers

    Perry

    Friday, August 8, 2014 7:30 PM
  • In this instance, no, although I have an option to read all unread emails at startup.

    Once up and running, the outlook addin only triggers when a new email is received.

    Naturally, these can be followed in quick succession, however, when I re restart outlook, to process any unread, the same email would appear to be successfully processed, with the exception of the last 3 statements, eg. Delete, and now havind added the unread and save, these two do not work but delete does work.

    I can only think its something to do with email size?

    Perry

    Friday, August 8, 2014 7:41 PM
  • Do you use the cached exchange mode?

    Could you please show your all code where you get the item and then release it (all the code)?

    Saturday, August 9, 2014 7:47 AM
  • Could you please show your all code where you get the item and then release it (all the code)?

    Saturday, August 9, 2014 7:48 AM
  • Hi Eugene

    Hopefully this gives you enough info

    I do different things depending on whether the email is internal of external.

    The below if the core processing unit/routines, but others may open the message again using the Message ID

    Thanks for your time

    Perry

        Private Sub Items_ItemAdd(ByVal item As Object) Handles items.ItemAdd
    
                If TypeOf (item) Is Outlook.MailItem Then
                    Dim mail As Outlook.MailItem = item
    
                    MessageID = mail.EntryID
                    Message_Control_Manager()
                End If
    
        End Sub
    
    
        Private Sub Message_Control_Manager()
    
            mail = outlookNameSpace.GetItemFromID(MessageID)
    
            If mail.MessageClass <> "IPM.Note" Then
                Dim destFolder As Outlook.MAPIFolder = inbox.Folders("Non-IPM Note Messages")
                mail.Move(destFolder)
            Else
                msgSubject = mail.Subject
                msgBody = mail.Body
                msgSenderName = mail.SenderName
                msgTrueSenderName = mail.SenderName
                msgSenderEmailAddress = mail.SenderEmailAddress
    
                If mail.SenderEmailType = "EX" Then
                    If outlookNameSpace.CurrentUser.Name <> mail.SenderName Then
                        Tag_Control()
    
                        If flgMessageOK = True Then
                            If flgDeleteMsg = False Then
                                mail.Importance = Outlook.OlImportance.olImportanceLow
                            Else
                                mail.UnRead = False
                                mail.Save()
                                mail.Delete()
                            End If
                        Else
                            Filing_Error_Internal()
                            Dim destFolder As Outlook.MAPIFolder = inbox.Folders("Messages Agent failed to file")
                            mail.Move(destFolder)
                        End If
                    End If
                Else
                    REM mail.SenderEmailType = "SMTP" 'External/internet email in
                    Tag_Control()
    
                    If flgMessageOK = False Then
                        Filing_Error_External()
                        If flgKeepSMTPMsgInInbox = True Then
                        Else
                            Dim destFolder As Outlook.MAPIFolder = inbox.Folders("Messages Agent failed to file")
                            mail.Move(destFolder)
                        End If
                    Else
                        If flgDeleteMsg = False Then
                            mail.Importance = Outlook.OlImportance.olImportanceLow
                        Else
                            mail.UnRead = False
                            mail.Save()
                            mail.Delete()
                        End If
                    End If
                End If
            End If
    
        End Sub
    


    Saturday, August 9, 2014 8:16 AM
  • Why do you need to use the GetItemFromID method for getting a new instance of the mail item if you have already got a reference?

    Did you try to copy the mail to the destination folder and then delete the original one? Does it help?

    Also I don't see any statements for releasing underlying COM objects in the code. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.

    Saturday, August 9, 2014 8:41 AM
  • Hi Pgarrod,

    What I thought is that you could log all actions into a specified place such as a Text document. After that, when you got some issues or different result, you could check the log.

    Hope this helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 11, 2014 7:36 AM
    Moderator
  • Hi Eugene

    Added the "housekeeping" to release objects etc., but then ran an issue

    The triggering of new emails did not work.  Works when I dont clear down the Inbox and Items.

    Are these exceptions to the "housekeeping rule" or should they be treated differently?

    Thanks
    Perry

        Dim outlookNameSpace As Outlook.NameSpace
        Dim inbox As Outlook.MAPIFolder
        Dim mail As Outlook.MailItem
        Dim WithEvents items As Outlook.Items


        Private Sub Items_ItemAdd(ByVal item As Object) Handles items.ItemAdd

            If TypeOf (item) Is Outlook.MailItem Then
                Dim mail As Outlook.MailItem = item

                If mail.UnRead Then
                    MessageID = mail.EntryID
                    FA_MailItem = mail
      PROCESSSING
                End If
     End If 
      
            If Not IsNothing(inbox) Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inbox)
                inbox = Nothing
            End If
            If Not IsNothing(items) Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(items)
                items = Nothing
            End If

    Tuesday, August 12, 2014 11:54 AM
  • The items and inbox objects shouldn't affect the NewMailEx event. Releasing the items object could prevent the ItemAdd event from firing.
    Did you try to debug the code and see what's happening in the NewMailEx event handler?
    Tuesday, August 12, 2014 12:13 PM