-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.fsx
33 lines (27 loc) · 888 Bytes
/
mail.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#r "nuget: MailKit"
#r "nuget: FSharp.Data"
open MimeKit
open MailKit.Net.Smtp
open FSharp.Data
type SmtpConfig = JsonProvider<const(__SOURCE_DIRECTORY__ + "/smtp.json")>
let client (it: SmtpConfig.Root) =
fun f ->
use client = new SmtpClient()
client.Connect(it.Server, it.Port, true)
client.Authenticate(it.Sender, it.Password)
client |> f
let private body content =
let body = TextPart("plain")
body.Text <- content
body
let message (content:string) (subject:string) (toEmail:string) (fromEmail:string) (fromName:string) =
let mail = MimeMessage()
mail.Subject <- subject
mail.Body <- body content
mail.From.Add(MailboxAddress(fromEmail, fromName))
mail.To.Add(MailboxAddress(toEmail, toEmail))
mail
let sendEmail (client:SmtpClient) email =
try
client.Send email |> Ok
with e -> Error e