0 votes

Dear LimiLabs Team and forum members,

I am trying to download and save specific email attachments for which I am using a regular expression to match a specific pattern. I followed your instructions on how to download all emails which worked well.

However, the goal is to download only pdf-attachments that match a particular pattern. Am I on the wrong track?

Could you please provide a solution and an example on how to filter through attachments. As a starting point for your analysis, please consider the following code (Note I changed the regex a bit, but the pattern I am currently works well, I am more interested in the mechanics on how to properly apply the pattern(s) to filter out the desired pdf-files):

For Each uid As Long In uids

    Dim eml() As Byte = imap.GetMessageByUID(uid)
    Dim email As IMail = New MailBuilder().CreateFromEml(eml)

    For Each pdfattachment In email.Attachments
        For Each mime As MimeData In email.Attachments

           Static attachmentregex As New Regex("pre-.+?\s+.+?\s+.+?\s+?.+?\s+\d{8}\.pdf")

           If attachmentregex.IsMatch(mime.FileName) Then
               mime.Save(path2AttachmentFolder + mime.SafeFileName)
           End If
        Next
    Next
Next

Thanks a mil in advance
Sas

by (200 points)

1 Answer

0 votes

Your code is almost correct.

It has one loop too much and you should use Path.Combine instead of string concatenation:

For Each uid As Long In uids

    Dim eml() As Byte = imap.GetMessageByUID(uid)
    Dim email As IMail = New MailBuilder().CreateFromEml(eml)

    For Each attachment In email.Attachments

       Static attachmentregex As New Regex("pre-.+?\s+.+?\s+.+?\s+?.+?\s+\d{8}\.pdf")

       If attachmentregex.IsMatch(attachment.SafeFileName) Then
           attachment.Save(Path.Combine(path2AttachmentFolder, attachment.SafeFileName))
       End If
    Next
Next
by (298k points)
edited by
Dear Limilabs support
Thank you very much for your prompt answer and the solution you kindly provided. Something about your solutions still remains unclear. You suggested getting rid off the following line:
    For Each mime As MimeData In email.Attachments
However, if I am not using this line how am I supposed to be able to use
    mime.Save(Path.Combine(path2AttachmentFolder,  mime.SafeFileName))
As your solution suggested. I was working under the assumption that mime has to declared/initialised in order to make it work. Not using the mime loop results in my code being automatically converted into
   MIME.Save(Path.Combine(path2AttachmentFolder, MIME.SafeFileName))
saying that SafeFileName is not a member of MIME. I am using Visual Studio 2010.
Could you please give me some feedback regarding your solution?
Thanks in advance for your reply
See my edit: forgot to change all mime -> attachment.
Thanks a lot for your explanation. Apparently my last comment had not been saved. Despite the strings and objects being correct so far, the attachments are not saved. I am not getting any error messages or anything like it. I checked the variables in Debug-Mode, too (I am using VS 2010), but I could not find a clue. I am using this code fragment now for the regex outlined in the beginning (regex also still is OK). Just would like to make sure that there isn't a logical error in the code.
Thanks a lot for your support

For Each uid As Long In uids

                Dim eml() As Byte = imap.GetMessageByUID(uid)
                Dim email As IMail = New MailBuilder().CreateFromEml(eml)
                txtBoxEmails.AppendText(email.Text)

                For Each attachment In email.Attachments
                    Static attachmentregex As New Regex("pre-.+?\s+.+?\s+.+?\s+?.+?\s+\d{8}\.pdf")
                    If attachmentregex.IsMatch(attachment.SafeFileName) Then
                        attachment.Save(Path.Combine(path2AttachmentFolder, attachment.SafeFileName))
                    End If
                Next
            Next
is IMail.Attachments collection non empty? What is the return value of Path.Combine(path2AttachmentFolder, attachment.SafeFileName)? Is it correct path? please remove 'if attachmentregex.IsMatch' line - are attachments saved then?
email.Attachments has 2 items of type {application/pdf}. Both name(s) withing the FileName property correspond to the desired pdf name.
Removing the if+regex line as suggested resulted in all attachments being downloaded. I can assure that the regex is correct since I have been running it in several other applications. Do you have any further suggestions about how to filter through the attachments or did I make mistake at another stage. Please excuse my question I am quite new to coding.
Thanks a mil for your reply
This means that your regex doesn't match the SafeFileName. You need to change the regex.
Thank you very much, glad we could rule out any other logical or programm issues. I will examine my regex. Thanks a lot for your support in this matter. Your help was much appreciated.
Kind regards
Sas
...