locked
Outlook attachments are being resent to my attachment location RRS feed

  • Question

  • Hi i have built an outlook 2010 addin in vb.net that has to extract attachments to a specific folder on my disk, this work fine...

    My code that works:

        Private Sub ThisAddIn_NewMail() Handles Application.NewMail
            Dim inBox As Outlook.MAPIFolder = Me.Application.ActiveExplorer(). _
                Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            Dim inBoxItems As Outlook.Items = inBox.Items
    
            Dim newEmail As Outlook.MailItem
            inBoxItems = inBoxItems.Restrict("[Unread] = true")
            Try
                For Each collectionItem As Object In inBoxItems
                    newEmail = TryCast(collectionItem, Outlook.MailItem)
                    If newEmail IsNot Nothing Then
                        If newEmail.Attachments.Count > 0 Then
                            For i As Integer = 1 To newEmail.Attachments.Count
                                Dim saveAttachment As Outlook.Attachment = newEmail.Attachments(i)
                                newEmail.Attachments(i).SaveAsFile("C:\attachments\" & (GetSenderSMTPAddress(newEmail)) _
                                & "_" & ((DateTime.Now).ToString("yyyy-MM-dd-HH-mm-ss")) & "_" & (newEmail.Attachments(i).FileName))
                            Next i
                        End If
                    End If
                Next collectionItem
            Catch ex As System.Exception
                If Left(ex.Message, 11) = "Cannot save your attachment" Then
                    MsgBox("Create Folder C:\attachments")
                End If
    
            Finally
                newEmail = Nothing
                inBoxItems = Nothing
            End Try
        End Sub

    But NOW it seems to me that Outlook 2010 saves an example of the previous attachments in memory and sends them along to my inbox and attachment location when the next Email with attachments comes in, this means that it´s actually saving almost everything double up...

    Is this an Outlook thing or could it be my code???

    I am really stuck on this so if you could give me an answer, maybe followed by an example if it exists THAT would be great thanks ;o)

    Wednesday, March 20, 2013 11:49 PM

Answers

  • it is your code. You filter by unread mails but after you are done processing that mail, you do not set its Unread property to false and save it.

    There are also other problems with your code - you do not call Marshal.ReleaseComObject on Outlook objects that you are done with and initialize  saveAttachment variable for nothing, just to use newMail.Attachments(i) in the Next line, please fix it.

    • Marked as answer by øv Friday, March 22, 2013 11:40 AM
    Thursday, March 21, 2013 7:47 AM
  • put it when you are done processing mail, which Means after saving attachments to disk. As for SaveAsFile - call it on saveAttachment variable
    • Marked as answer by øv Friday, March 22, 2013 11:40 AM
    Thursday, March 21, 2013 2:43 PM

All replies

  • it is your code. You filter by unread mails but after you are done processing that mail, you do not set its Unread property to false and save it.

    There are also other problems with your code - you do not call Marshal.ReleaseComObject on Outlook objects that you are done with and initialize  saveAttachment variable for nothing, just to use newMail.Attachments(i) in the Next line, please fix it.

    • Marked as answer by øv Friday, March 22, 2013 11:40 AM
    Thursday, March 21, 2013 7:47 AM
  • I can see the point in setting the unread mails ito false but where do i put it?

    inBoxItems = inBoxItems.Restrict("[Unread] = False") + newEmail.Save()

    Marshal.ReleaseComObject, i´ll look into that...

    You are right about that the saveAttachment variable is set and not used but when i remove it the code definetly doesn´t work!

    I´ve tried : saveAttachments.SaveAsFile(….) didn´t work

    what am i missing for this code to just work??

    Thursday, March 21, 2013 1:03 PM
  • put it when you are done processing mail, which Means after saving attachments to disk. As for SaveAsFile - call it on saveAttachment variable
    • Marked as answer by øv Friday, March 22, 2013 11:40 AM
    Thursday, March 21, 2013 2:43 PM
  • Thank you DamianD it works perfectly now ;o)
    • Edited by øv Friday, March 22, 2013 11:41 AM
    Friday, March 22, 2013 11:40 AM
  • Hi DamianD. How & where would you place the Marshal.ReleaseComObject? I'm not clear about which objects should be released within the "For" loop and which should be released after the loop. Thanks!
    Friday, June 21, 2013 7:14 PM
  • saveAttachment just before 'next i' line, attachments collection right after looping it, newEmail right before 'next collectionItem'
    Saturday, June 22, 2013 10:51 AM