F # Email Listener

I would like to make an email listener that listens for an imap mailbox. My main idea is to simply track the server using the imap client, how to create such a loop in f # and how I can turn the program into a service that can be started and stopped. Also, is there a better way to listen to the imap server than polling it every few seconds?

EDIT

I found this question that answers my questions about creating a service: Writing a Windows Polling Service . However, the question still remains if this is the right way to listen to the IMAP mailbox. Will there be any probable problems?

EDIT 2

I looked at S22.Imap, which uses an event handler in C #, but I'm not sure how to implement this in F #, basically this line:

// We want to be informed when new messages arrive Client.NewMessage += new EventHandler<IdleMessageEventArgs>(OnNewMessage); 

I assume that the program is so supported, does it sit on this line, waiting for events?

The code author also posted this under this, which I don't quite understand:

 // Put calling thread to sleep. This is just so the example program does // not immediately exit. System.Threading.Thread.Sleep(60000); 

Can someone explain to me how I can implement this in F #? A complete example is available here .

Edit 3

Not sure why this particular question was closed, perhaps the last sentence of Edit 2 was not read. So my question is:

Can someone explain to me how I can handle event handling in F #? I can not find anything on Google that is not for graphical applications. How can I give an example of the C # code above into something in F #?

Any explanation of downvotes would also be appreciated, my original question was partially answered by some people, and I found some answers for myself, but he did not get a complete answer, the question just turned into something a little different.

+4
source share
1 answer

It seems that the API you are using makes this extremely simple. You do not need to do your own polling, as the IMAP client provides the NewMessage event. The example you referenced (EDIT 2) is a console application, so calling Thread.Sleep does not allow it to exit immediately. This is not a problem in the Windows service. This process will be supported by the service manager. The following code should serve as the basis for your service.

 open S22.Imap type ImapService() = inherit System.ServiceProcess.ServiceBase() let mutable client = null override __.OnStart(_) = client <- new ImapClient("imap.gmail.com", 993, "username", "password", AuthMethod.Login, true) client.NewMessage.Add(fun args -> let msg = client.GetMessage(args.MessageUID, FetchOptions.HeadersOnly) //do something with msg () ) override __.OnStop() = client.Dispose() 
+4
source

Source: https://habr.com/ru/post/1447833/


All Articles