Programmatically open an email from a POP3 and extract an attachment

11,422

Solution 1

possible duplication of Reading Email using Pop3 in C#

Atleast, there's a shed load of suggestions there that you may find useful

Solution 2

Try Mail.dll email component, it's very affordable, supports attachments national characters and is easy to use, it also supports SSL:

Using pop3 As New Pop3()
    pop3.Connect("mail.server.com") 
    pop3.Login("user", "password")                            

    Dim builder As New MailBuilder()
    For Each uid As String In pop3.GetAll()           
        ' Receive email message'
        Dim mail As IMail = builder.CreateFromEml(pop3.GetMessageByUID(uid))

        'Write out received message'
        Console.WriteLine(mail.Subject)

        'Here you can use mail.Attachmets collection'
        For Each attachment As MimeData In mail.Attachments
            Console.WriteLine(attachment.FileName)
            attachment.Save("c:\" + attachment.FileName)
            ' you can also use attachment.Data here'
        Next attachment

    Next

    pop3.Close(true)   
End Using

You can download it here: http://www.lesnikowski.com/mail.

Solution 3

I'll throw in a late suggestion for a more generalized "download POP3 messages and extract attachments" solution using existing software and minimal programming. I needed to do this for a client who switched to receiving faxes via email and was not pleased with manually saving the attachments to a location where they could be imported into an application.

For downloading messages on *nix systems fetchmail seems to be the standard and is very capable, but I chose mpop for both simplicity and Windows compatibility (but it is cross-platform). If mpop hadn't done the trick for me, I probably would have ended up doing something with the Python-based getmail, which was created when fetchmail's development stalled for a time (it's since resumed).

Mpop is controlled either via command line or configuration file, so I simply created multiple configuration files and specify via command line which file to load. I'm using it in "Exchange pickup directory" mode, which means it simply downloads the messages and drops them as text (.eml) files in a specified directory.

For extraction of the message attachments, UUDeview appears to be the standard (I'm using the Windows port of UUDeview) across just about any system you could want with just about any features you could want. My main alternative to this was a much-less-capable Python script that I'd developed for a different client back in 2007, but I'm happy to go with a precompiled executable over either installing Python or packaging with any of the Python-to-exe options.

Finally there's the configuration - along with the two mpop configuration files mentioned above (which I could do away with by using command-line options), I also have two 2-line .cmd files launched every 10 minutes by scheduled task - the first line to launch mpop to download into a working directory and the second line to launch UUDeview and extract attachments of specified types (.pdf or .tif) then delete each file from which it extracted attachments. Output is sent to another directory from which staff can directly attach files as needed.

This is overall not the most elegant way to reach these ends, but it was quick, simple, functional and reasonably robust - at each stage if something goes wrong it fails such that no data is lost. The only places where data could be lost are any non-attachment messages being sent to the dedicated fax email addresses, and even those will sit in the processing directory and be caught eventually.

Share:
11,422
Josh
Author by

Josh

Updated on June 15, 2022

Comments

  • Josh
    Josh about 2 years

    We have a vendor that sends CSV files as email attachments. These CSV files contain statuses that are imported into our application. I'm trying to automate the process end-to-end, but it currently depends on someone opening an email, saving the attachment to a server share, so the application can use the file.

    Since I cannot convince the vendor to change their process, such as offering an FTP location or a Web Service, I'm stuck with trying to automate the existing process.

    Does anyone know of a way to programmatically open an email from a POP3 account and extract an attachment? The preferred solution would reside on a Windows 2003 server, be written VB.NET and secure. The application can reside on the same server as the POP3 server, for example, we could setup the free POP3 server that comes with Windows Server and pull against the mail file stored on the file system.

    BTW, we are willing to pay for an off-the-shelf solution, if one exists.

    Note: I did look at this question but the answer points to a CodeProject solution that doesn't deal with attachments.