Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Create, save, and reopen a plain-text email

See more Email Object Examples

Set the sender, recipient, subject, and plain-text body. Save the result as an EML file, then open it in a second Email object and read its contents.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    let email = CkoEmail()!
    email.from = "Sam Lee <sam@example.com>"
    success = email.add(to: "Alex Rivera", emailAddress: "alex@example.net")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    email.subject = "Hello"
    email.body = "This is a plain-text message."
    success = email.saveEml(path: "message.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    let received = CkoEmail()!
    success = received.loadEml(mimePath: "message.eml")
    if success != true {
        print("\(received.lastErrorText!)")
        return
    }

    print("\(received.from!)")
    print("\(received.subject!)")
    print("\(received.getPlainTextBody()!)")

}