5 people like it.

Sending an email

Here's how to send an email through SMTP. Works from FSI provided you are an authenticated user for the SMTP server. (Vandalism reverted.)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
module Demo =
    open System.Net.Mail
        let SendTest() =
            let msg = new MailMessage("me@mydomain.com", @"you@yourdomain.com", @"A test", 
                                        "The lesser world was daubed\nBy a colorist of modest skill.\n\
                                        A master limned you in the finest inks\nAnd with a fresh-cut quill.\n")
            msg.Bcc.Add(@"them@theirdomain.com")

            let client = new SmtpClient(@"smtpmail.xx.yyyy.com")

            let pretend = false
            if pretend then
                let pickupDir =  @"d:\temp\pretendmail"
                System.IO.Directory.CreateDirectory(pickupDir) |> ignore
                client.DeliveryMethod <- SmtpDeliveryMethod.SpecifiedPickupDirectory;
                client.PickupDirectoryLocation <- pickupDir
            else
                client.DeliveryMethod <- SmtpDeliveryMethod.Network

            client.Send(msg)
namespace System
namespace System.Net
namespace System.Net.Mail
val SendTest : unit -> unit

Full name: Script.Demo.SendTest
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.Bcc: MailAddressCollection
System.Collections.ObjectModel.Collection.Add(item: MailAddress) : unit
MailAddressCollection.Add(addresses: string) : unit
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
val pretend : bool
val pickupDir : string
namespace System.IO
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
System.IO.Directory.CreateDirectory(path: string) : System.IO.DirectoryInfo
System.IO.Directory.CreateDirectory(path: string, directorySecurity: System.Security.AccessControl.DirectorySecurity) : System.IO.DirectoryInfo
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
property SmtpClient.DeliveryMethod: SmtpDeliveryMethod
type SmtpDeliveryMethod =
  | Network = 0
  | SpecifiedPickupDirectory = 1
  | PickupDirectoryFromIis = 2

Full name: System.Net.Mail.SmtpDeliveryMethod
field SmtpDeliveryMethod.SpecifiedPickupDirectory = 1
property SmtpClient.PickupDirectoryLocation: string
field SmtpDeliveryMethod.Network = 0
SmtpClient.Send(message: MailMessage) : unit
SmtpClient.Send(from: string, recipients: string, subject: string, body: string) : unit

More information

Link:http://fssnip.net/eg
Posted:8 years ago
Author:Kit Eason
Tags: email , smtp