How to implement MailProcess application in F#

Here in this Blog I am showing that how to implement MailProcess application in F#. An Agent is defined in the code for Reading Mail. Here Mail is a normal String which can be read from the Console Application Window.

Example


module
EmailSendingProcedure =
 
    open System
 
    type Msg = string * AsyncReplyChannel<string>
    let textStr = "Email {0} was received. Email contents: {1}"
    let agent = MailboxProcessor<Msg>.Start(fun mailbox ->
        let rec loop x =
            async {
                    let! (message, replyChannel) = mailbox.Receive();
                    if (message = "Exit") then
                        replyChannel.Reply("Exitting.")
                    else
                        replyChannel.Reply(String.Format(textStr, x, message))
                    do! loop (x + 1)
            }
        loop 0)
 
    printfn "Email Sending Process"
    printfn "Write Text and Press ENTER to Send a Mail."
    printfn "Write 'End' to Exit."
 
    let mutable exit = false
    while (not exit) do
        printf "> "
        let input = Console.ReadLine()
        let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
        if (reply = "Exitting.") then
            exit <- true
        printfn "Reply: %s" reply
        printfn "If you want to continue press ENTER."
    Console.ReadLine() |> ignore

Output

MailProcess Example Output

Ebook Download
View all
Learn
View all