Sample code for 30+ languages & platforms
Swift

MX Lookup Mail Server Domain by Email Address

How to find the mail server for a given email address. Returns the domain name of the primary mail server.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let dns = CkoDns()!
    let json = CkoJsonObject()!

    var emailAddr: String? = "bob@example.com"

    // This gets all MX domains for an email address.  (Typically one domain.)
    // The preferred domain will be at index 0 (see below).
    success = dns.query(recordType: "MX", domain: emailAddr, answer: json)
    if success == false {
        print("\(dns.lastErrorText!)")
        return
    }

    var i: Int = 0
    var count_i: Int = json.size(ofArray: "answer.mx").intValue
    while i < count_i {
        json.i = i
        var domain: String? = json.string(of: "answer.mx[i].domain")
        print("\(domain!)")
        i = i + 1
    }


}