Sample code for 30+ languages & platforms
PureBasic

Create Email with Non-Standard Binary Body

Creates an email where the only body is a binary WAV file. The technique used in the example could be applied to other binary files, such as PDF, MS-WORD docs, Excel docs, etc.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

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

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckSetBodyFromFile(mime,"VoiceMessage.wav")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ; The MIME has this header:
    ; Content-Disposition: attachment;
    ; 	 filename="VoiceMessage.wav"
    ; Content-Transfer-Encoding: base64
    ; Content-Type: audio/x-wav;
    ; 	name="VoiceMessage.wav"

    ; We don't want the content-disposition to be an
    ; attachment -- otherwise the email object will self-correct
    ; and put it in a multipart/mixed format...
    CkMime::setCkDisposition(mime, "")
    CkMime::setCkFilename(mime, "")

    strMime.s
    strMime = CkMime::ckGetMime(mime)
    Debug strMime

    ; Now load it into an email object:
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckSetFromMimeText(email,strMime)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkMime::ckDispose(mime)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; Add subject, TO, FROM, etc.
    CkEmail::setCkSubject(email, "This is a test")
    CkEmail::setCkFrom(email, "support@chilkatsoft.com")
    success = CkEmail::ckAddTo(email,"Matt","matt@chilkatsoft.com")

    ; Your email is ready to send.
    ; (but for this example, we'll simply save it to a file...)
    success = CkEmail::ckSaveEml(email,"email.eml")
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkMime::ckDispose(mime)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "OK!"


    CkMime::ckDispose(mime)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure