3

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.

  • Is supposed to run on a desktop? with a client (outlook, etc.)? or on a server? which server (exchange, etc.)? – Simon Mourier Jun 04 '17 at 17:02
  • @SimonMourier: actually it can be both. Outlook client, or some server (I don't have the info right now). Answer for an Outlook client would already be helpful. –  Jun 04 '17 at 17:10
  • You can use Outlook's Application.NewMailEx event from C# (like in VBA here: https://msdn.microsoft.com/en-us/library/office/aa171304.aspx). Depending on your configuration (security, mail account types, etc.) the implementation details may vary considerably. – Simon Mourier Jun 04 '17 at 17:25
  • Why don't you answer comments? – Simon Mourier Jun 10 '17 at 07:48
  • @SimonMourier: I did answer your first comment, didn't I ? –  Jun 10 '17 at 09:24

1 Answers1

1

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

user9993
  • 5,833
  • 11
  • 56
  • 117