Sample code for 30+ languages & platforms
Swift

Build a multipart/related MIME Message

See more MIME Examples

Demonstrates how to construct a multipart/related MIME message containing HTML and a related JPG image.

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.

    // Start with an empty MIME object...
    let mime = CkoMime()!

    // Initialize it as multipart/related.
    mime.newMultipartRelated()

    // Set a top-level MIME header field.
    mime.setHeaderField(name: "Subject", value: "Test HTML email with JPG image.")

    // Generate a Content-ID.  This will contain random data that will allow
    // the HTML's "img" tag to reference the image part in the MIME.
    let crypt = CkoCrypt2()!
    crypt.encodingMode = "hex"

    let sbContentId = CkoStringBuilder()!
    sbContentId.append(value: "part1.")
    sbContentId.append(value: crypt.genRandomBytesENC(numBytes: 4))
    sbContentId.append(value: ".")
    sbContentId.append(value: crypt.genRandomBytesENC(numBytes: 4))
    sbContentId.append(value: "@chilkatsoft.com")

    // Create the HTML body.
    let sbHtml = CkoStringBuilder()!
    sbHtml.append(value: "<html><head>\r\n")
    sbHtml.append(value: "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n")
    sbHtml.append(value: "  </head>\r\n")
    sbHtml.append(value: "  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n")
    sbHtml.append(value: "    <p>This is a test.&nbsp; <img src=\"cid:CONTENT_ID\" height=\"20\" width=\"20\"></p>\r\n")
    sbHtml.append(value: "  </body>\r\n")
    sbHtml.append(value: "</html>\r\n")
    var numReplacements: Int = sbHtml.replace(value: "CONTENT_ID", replacement: sbContentId.getAsString()).intValue

    // Create the HTML MIME part.
    let htmlMime = CkoMime()!
    htmlMime.setBody(fromHtml: sbHtml.getAsString())
    htmlMime.encoding = "8bit"
    htmlMime.charset = "utf-8"

    // Load our JPG image from a file.
    let fac = CkoFileAccess()!
    var jpgBytes: NSData
    jpgBytes = fac.readEntireFile(path: "qa_data/jpg/starfish20.jpg")

    // Create an image/jpeg MIME part.
    let jpgMime = CkoMime()!
    jpgMime.setBody(fromBinary: jpgBytes)
    // Setting the following properties has the effect of adding
    // the Content-Type, Content-Transfer-Encoding, and Content-Disposition header fields.
    // These header fields could also be set via the SetHeaderField method, in which case
    // the values of the affected properties would be updated.
    jpgMime.contentType = "image/jpeg"
    jpgMime.name = "starfish20.jpg"
    jpgMime.encoding = "base64"
    jpgMime.disposition = "inline"
    jpgMime.filename = "starfish20.jpg"

    let sbContentIdHdr = CkoStringBuilder()!
    sbContentIdHdr.append(value: "<CONTENT_ID>")
    numReplacements = sbContentIdHdr.replace(value: "CONTENT_ID", replacement: sbContentId.getAsString()).intValue
    jpgMime.setHeaderField(name: "Content-ID", value: sbContentIdHdr.getAsString())

    // Add both parts as children of the multipart/related
    mime.appendPart(mime: htmlMime)
    mime.appendPart(mime: jpgMime)
    print("\(mime.getMime()!)")

    // --------------------------------------------------------------
    // --------------------------------------------------------------
    // This example builds the following MIME message.  The boundary
    // and Content-ID strings contain random data and will be different.  

    // Content-Type: multipart/related; boundary="------------A940F1230E6F0105F03DB2CB"
    // Subject: Test HTML email with JPG image.
    // 
    // --------------A940F1230E6F0105F03DB2CB
    // Content-Type: text/html; charset="utf-8"
    // Content-Transfer-Encoding: 8bit
    // 
    // <html><head>
    // <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    //   </head>
    //   <body bgcolor="#FFFFFF" text="#000000">
    //     <p>This is a test.&nbsp; <img src="cid:part1.E16AE3B4.1505C436@chilkatsoft.com" height="20" width="20"></p>
    //   </body>
    // </html>
    // 
    // --------------A940F1230E6F0105F03DB2CB
    // Content-Transfer-Encoding: base64
    // Content-Type: image/jpeg; name="starfish20.jpg"
    // Content-Disposition: inline; filename="starfish20.jpg"
    // Content-ID: <part1.E16AE3B4.1505C436@chilkatsoft.com>
    // 
    // /9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBob3Rvc2hvcD8g
    // NC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdKDozPTw5Mzg3QEhcTkBEV0U3OFBtUVdf
    // YmdoZz5NcXlwZHhcZWdj/9sAQwEREhIYFRgvGhovY0I4QmNjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj
    // Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj/8IAEQgAFAAUAwERAAIRAQMRAf/EABcAAAMBAAAA
    // AAAAAAAAAAAAAAIDBAX/xAAYAQADAQEAAAAAAAAAAAAAAAABAgMEAP/aAAwDAQACEAMQAAAB2kZY
    // NNEijWKddfTmLgALWH//xAAbEAACAgMBAAAAAAAAAAAAAAABAgMRAAQSE//aAAgBAQABBQL0XqN+
    // pM2aqJGMiqFFCyg7z//EABwRAAICAgMAAAAAAAAAAAAAAAERAAIQIQMSUf/aAAgBAwEBPwHqU5aq
    // Axx+y1tMQl4elj//xAAcEQEAAQUBAQAAAAAAAAAAAAABEQACEBIhA1H/2gAIAQIBAT8B3Bhqy7Zc
    // enyiwmGgDhiOzj//xAAdEAABAwUBAAAAAAAAAAAAAAABAAIREBIhIkFR/9oACAEBAAY/ArZyn+Cg
    // xtxWuJaoCnqDuin/xAAcEAABBAMBAAAAAAAAAAAAAAABABEhYRAxQVH/2gAIAQEAAT8hkEwPUUR9
    // DYfE4nxtRpIkBTsayuALIiuY/9oADAMBAAIAAwAAABDWPTsf/8QAGhEAAwADAQAAAAAAAAAAAAAA
    // AAEREDFBIf/aAAgBAwEBPxC0DVPcWm+Ce4OesrkE6bjH/8QAGBEBAQEBAQAAAAAAAAAAAAAAAREA
    // QRD/2gAIAQIBAT8QahMiOc8YgSrnTY3ELclHXn//xAAcEAEBAAIDAQEAAAAAAAAAAAABEQAhMUFx
    // EFH/2gAIAQEAAT8Qn3igmSZSj+c4N4zapMy9IjFV98wncN2iuLFsCEbDGxQkI6RO/n//2Q==
    // 
    // --------------A940F1230E6F0105F03DB2CB--
    // 

}