Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic processing of e-mail attachements

I want to write an application (most probably in C#) that checks e-mails on a particular account, detects attached files and detaches them to a folder for processing.

Are there standard .NET classes to perform these tasks ? If not, what else can I use ?

The application will run as a service.


1 Answers

While there are no API's in the BCL for downloading emails, only sending, there is a very well regarded that is now the Microsoft recommended library for sending and receiving email, it supports POP3, IMAP, SMTP. https://github.com/jstedfast/MailKit

detects attached files and detaches them to a folder for processing

I'm going to assume you mean download a file to a directory. Fortunately with MailKit this is very easy to do, and the author of the library has written an example here: https://stackoverflow.com/a/36229918/2595033

(code taken from link)

foreach (var attachment in message.Attachments) {
    using (var stream = File.Create ("fileName")) {
        if (attachment is MessagePart) {
            var part = (MessagePart) attachment;

            part.Message.WriteTo (stream);
        } else {
            var part = (MimePart) attachment;

            part.ContentObject.DecodeTo (stream);
        }
    }
}

The application will run as a service.

This is also very easy to do, you need to write a Windows Service. There's plenty of resources regarding writing one in C#. There's also a template for it in Visual Studio.

enter image description here

like image 116
user9993 Avatar answered Nov 26 '25 13:11

user9993



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!