iOS XCode send and read email example

Send and receive email in Swift using MailSlurp email API

How to send and receive email in Swift using MailSlurp email API.


MailSlurp is a library with iOS/iPadOS/MacOS/TVOS support via swift and objective C for sending an receiving emails from real email addresses in code an tests. Use MailSlurp to test iOS apps end-to-end using test email accounts or embed MailSlurp to send and receive emails from apps.

Install MailSlurp in XCode

To get started using MailSlurp first create a new XCode project


3 4 5 7 8 9 10 6

Alternative Package.swift installation

You can also use Package.swift and Swift Package Manager to install MailSlurp:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "example",
    // add platform as mailslurp requires macOS >= v10_11
    platforms: [
    dependencies: [
        // add mailslurp dependency
        .package(url: "", from: "12.4.2"),
    targets: [
        // add mailslurp to the target
            name: "example",
            dependencies: ["mailslurp"]),
            name: "exampleTests",
            dependencies: ["mailslurp"]),

Run swift package resolve to install dependencies.

Using MailSlurp in Swift code

The MailSlurp library creates controllers for every endpoint on the MailSlurp REST API

CommonActionsControllerAPI.sendEmailSimpleWithRequestBuilder(emailOptions: sendOptions)
  .addHeader(name: "x-api-key", value: apiKey)
  .done { response in
    // handle success
  .catch(policy: .allErrors) { err in
    // handle error
    guard let e = err as? mailslurp.ErrorResponse else {
        error = err.localizedDescription
    // pattern match the error to access status code and data
    // MailSlurp returns 4xx errors when invalid parameters or
    // unsatisfiable request. See the message and status code
    switch e {
    case .error(let statusCode, let data, _, _):
        let msg = String(decoding: data!, as: UTF8.self)
        error = "\(statusCode) Bad request: \(msg)"

Related content

MailSlurp SDKs for Swift and Objective C

Objective C email library. Email account API for Swift and ObjC development

Install Swift on Debian (Buster)

Install Swift on Debian (Buster)

iOS XCode send and read email example

Send and receive email in Swift using MailSlurp email API

Ready to dive in?Start building email applications today.