4 people like it.

Sending an email

Quite similar to http://www.fssnip.net/eg but authentication, html-messages and async sending.

 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: 
module Mail =

    open System
    open System.Net.Mail
    // open System.Configuration
    let server = "smtp.myserver.com" // ConfigurationManager.AppSettings.["mailserver"]
    let sender = "nospam@mydomain.com" // ConfigurationManager.AppSettings.["mailsender"]
    let password = "abc123" // ConfigurationManager.AppSettings.["mailpassword"] |> my-decrypt
    let port = 587
    let sendMailMessage email name topic msg =
        let msg = 
            new MailMessage(
                sender, email, topic, "Dear " + name + ", <br/><br/>\r\n\r\n" + msg)
        msg.IsBodyHtml <- true
        let client = new SmtpClient(server, port)
        client.EnableSsl <- true
        client.Credentials <- System.Net.NetworkCredential(sender, password)
        client.SendCompleted |> Observable.add(fun e -> 
            let msg = e.UserState :?> MailMessage
            if e.Cancelled then
                ("Mail message cancelled:\r\n" + msg.Subject) |> Console.WriteLine
            if e.Error <> null then
                ("Sending mail failed for message:\r\n" + msg.Subject + 
                    ", reason:\r\n" + e.Error.ToString()) |> Console.WriteLine
            if msg<>Unchecked.defaultof<MailMessage> then msg.Dispose()
            if client<>Unchecked.defaultof<SmtpClient> then client.Dispose()
        )
        // Maybe some System.Threading.Thread.Sleep to prevent mail-server hammering
        client.SendAsync(msg, msg)
namespace System
namespace System.Net
namespace System.Net.Mail
val server : string

Full name: Script.Mail.server
val sender : string

Full name: Script.Mail.sender
val password : string

Full name: Script.Mail.password
val port : int

Full name: Script.Mail.port
val sendMailMessage : email:string -> name:string -> topic:string -> msg:string -> unit

Full name: Script.Mail.sendMailMessage
val email : string
val name : string
val topic : string
val msg : string
val msg : MailMessage
Multiple items
type MailMessage =
  new : unit -> MailMessage + 3 overloads
  member AlternateViews : AlternateViewCollection
  member Attachments : AttachmentCollection
  member Bcc : MailAddressCollection
  member Body : string with get, set
  member BodyEncoding : Encoding with get, set
  member CC : MailAddressCollection
  member DeliveryNotificationOptions : DeliveryNotificationOptions with get, set
  member Dispose : unit -> unit
  member From : MailAddress with get, set
  ...

Full name: System.Net.Mail.MailMessage

--------------------
MailMessage() : unit
MailMessage(from: string, to: string) : unit
MailMessage(from: MailAddress, to: MailAddress) : unit
MailMessage(from: string, to: string, subject: string, body: string) : unit
property MailMessage.IsBodyHtml: bool
val client : SmtpClient
Multiple items
type SmtpClient =
  new : unit -> SmtpClient + 2 overloads
  member ClientCertificates : X509CertificateCollection
  member Credentials : ICredentialsByHost with get, set
  member DeliveryMethod : SmtpDeliveryMethod with get, set
  member Dispose : unit -> unit
  member EnableSsl : bool with get, set
  member Host : string with get, set
  member PickupDirectoryLocation : string with get, set
  member Port : int with get, set
  member Send : message:MailMessage -> unit + 1 overload
  ...

Full name: System.Net.Mail.SmtpClient

--------------------
SmtpClient() : unit
SmtpClient(host: string) : unit
SmtpClient(host: string, port: int) : unit
property SmtpClient.EnableSsl: bool
property SmtpClient.Credentials: Net.ICredentialsByHost
Multiple items
type NetworkCredential =
  new : unit -> NetworkCredential + 4 overloads
  member Domain : string with get, set
  member GetCredential : uri:Uri * authType:string -> NetworkCredential + 1 overload
  member Password : string with get, set
  member SecurePassword : SecureString with get, set
  member UserName : string with get, set

Full name: System.Net.NetworkCredential

--------------------
Net.NetworkCredential() : unit
Net.NetworkCredential(userName: string, password: string) : unit
Net.NetworkCredential(userName: string, password: Security.SecureString) : unit
Net.NetworkCredential(userName: string, password: string, domain: string) : unit
Net.NetworkCredential(userName: string, password: Security.SecureString, domain: string) : unit
event SmtpClient.SendCompleted: IEvent<SendCompletedEventHandler,ComponentModel.AsyncCompletedEventArgs>
module Observable

from Microsoft.FSharp.Control
val add : callback:('T -> unit) -> source:IObservable<'T> -> unit

Full name: Microsoft.FSharp.Control.Observable.add
val e : ComponentModel.AsyncCompletedEventArgs
property ComponentModel.AsyncCompletedEventArgs.UserState: obj
property ComponentModel.AsyncCompletedEventArgs.Cancelled: bool
property MailMessage.Subject: string
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
property ComponentModel.AsyncCompletedEventArgs.Error: exn
Exception.ToString() : string
module Unchecked

from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
MailMessage.Dispose() : unit
SmtpClient.Dispose() : unit
SmtpClient.SendAsync(message: MailMessage, userToken: obj) : unit
SmtpClient.SendAsync(from: string, recipients: string, subject: string, body: string, userToken: obj) : unit
Raw view Test code New version

More information

Link:http://fssnip.net/7PE
Posted:8 years ago
Author:Tuomas Hietanen
Tags: email , smtp