Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Create Binary MIME

Demonstrates how to create and save a multipart/mixed MIME document where the parts (a JPG and a PDF) are NOT base64 encoded, but are instead binary.

Note: This example requires Chilkat v9.5.0.62 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoMime
    Boolean iSuccess
    Variant vJpgPart
    Handle hoJpgPart
    Variant vPdfPart
    Handle hoPdfPart
    Variant vBinData
    Handle hoBinData
    Handle hoEmail
    Variant vSb
    Handle hoSb

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

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    Get ComSetBodyFromPlainText Of hoMime "This is the plain text body." To iSuccess
    Get ComConvertToMultipartMixed Of hoMime To iSuccess
    Get ComAppendPartFromFile Of hoMime "qa_data/jpg/penguins.jpg" To iSuccess
    Get ComAppendPartFromFile Of hoMime "qa_data/pdf/fishing.pdf" To iSuccess

    // At this point, when saved, the MIME bodies will be base64 encoded.
    Get ComSaveMime Of hoMime "qa_output/sample.txt" To iSuccess

    // We now have the following MIME where everything is base64 encoded:
    // The code that follows shows how to eliminate the base64 to make this binary MIME.

    // 	Content-Type: multipart/mixed; boundary="------------000207060703080505060404"
    // 
    // 	--------------000207060703080505060404
    // 	Content-Type: text/plain
    // 	Content-Transfer-Encoding: 7bit
    // 
    // 	This is the plain text body.
    // 	--------------000207060703080505060404
    // 	Content-Disposition: attachment; filename="penguins.jpg"
    // 	Content-Type: image/jpeg; name="penguins.jpg"
    // 	Content-Transfer-Encoding: base64
    // 
    // 	/9j/4AAQSkZJRgABAgEAYABgAAD/7gAOQWRvYmUAZAAAAAAB/+ESCEV4aWYAAE1NACoAAAAIAAcB
    // 	MgACAAAAFAAAAGIBOwACAAAABwAAAHZHRgADAAAAAQAEAABHSQADAAAAAQA/AACcnQABAAAADgAA
    // 	...
    // 	800a1MlLipJHlyU9en7sqVPkBK+gBj+o+1E91Ld7iJk0pJDO5PmDk4FOGOHy6S3JW120W1uCJ5M0
    // 	PBa54edOFAc8ePX/2Q==
    // 
    // 	--------------000207060703080505060404
    // 	Content-Disposition: attachment; filename="fishing.pdf"
    // 	Content-Type: application/pdf; name="fishing.pdf"
    // 	Content-Transfer-Encoding: base64
    // 
    // 	JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k
    // 	ZT4+CnN0cmVhbQp4nM1c288cNxVX09A0myq35tom7bSl8E1hp76P/YpASIiXlEg8tDwVKEJfilIe
    // 	...
    // 	MDRGMT48OTlENkRFQzExQjkzNjA0Mjc1RUFCNzIyMjI4RjA0RjE+XQo+PgpzdGFydHhyZWYKMjk0
    // 	MzY5CiUlRU9GCg==
    // 
    // 	--------------000207060703080505060404--
    // 

    // To make it binary MIME (getting rid of the base64), set the Encoding property to "binary"
    // for the JPG and PDF parts.
    Get ComGetPart Of hoMime 1 To vJpgPart
    If (IsComObject(vJpgPart)) Begin
        Get Create (RefClass(cComChilkatMime)) To hoJpgPart
        Set pvComObject Of hoJpgPart To vJpgPart
    End
    Set ComEncoding Of hoJpgPart To "binary"
    Send Destroy of hoJpgPart

    Get ComGetPart Of hoMime 2 To vPdfPart
    If (IsComObject(vPdfPart)) Begin
        Get Create (RefClass(cComChilkatMime)) To hoPdfPart
        Set pvComObject Of hoPdfPart To vPdfPart
    End
    Set ComEncoding Of hoPdfPart To "binary"
    Send Destroy of hoPdfPart

    // Now save it.  If you try to view this MIME in a text editor,
    // the JPG and PDF parts will be garbled and unintelligible. That's because
    // the bytes do not represent characters.
    Get ComSaveMime Of hoMime "qa_output/sampleBinary.mim" To iSuccess

    // The MIME now contains this:

    // 	Content-Type: multipart/mixed; boundary="------------000207060703080505060404"
    // 
    // 	--------------000207060703080505060404
    // 	Content-Type: text/plain
    // 	Content-Transfer-Encoding: 7bit
    // 
    // 	This is the plain text body.
    // 	--------------000207060703080505060404
    // 	Content-Disposition: attachment; filename="penguins.jpg"
    // 	Content-Type: image/jpeg; name="penguins.jpg"
    // 	Content-Transfer-Encoding: binary
    // 
    // 	<Binary Data Here>
    // 
    // 	--------------000207060703080505060404
    // 	Content-Disposition: attachment; filename="fishing.pdf"
    // 	Content-Type: application/pdf; name="fishing.pdf"
    // 	Content-Transfer-Encoding: binary
    // 
    // 	<Binary Data Here>
    // 
    // 	--------------000207060703080505060404--
    // 

    // Can we load this binary MIME into an Email object?
    Get Create (RefClass(cComChilkatBinData)) To hoBinData
    If (Not(IsComObjectCreated(hoBinData))) Begin
        Send CreateComObject of hoBinData
    End
    // Write the binary MIME into binData;
    Get pvComObject of hoBinData to vBinData
    Get ComGetMimeBd Of hoMime vBinData To iSuccess

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    // Load the email from the binData.
    Get pvComObject of hoBinData to vBinData
    Get ComSetFromMimeBd Of hoEmail vBinData To iSuccess

    // Note: Many email clients may not be able to correctly process emails
    // using the binary encoding.  Thunderbird has trouble.  Windows Live Mail
    // worked OK.
    Set ComSubject Of hoEmail To "Binary MIME Email"
    Set ComFrom Of hoEmail To "admin@chilkatsoft.com"
    Get ComAddTo Of hoEmail "Chilkat" "support@chilkatsoft.com" To iSuccess
    Get ComSaveEml Of hoEmail "qa_output/binaryEmail.eml" To iSuccess

    // Chilkat does not recommend trying to use binary MIME for email.
    // Binary MIME is typically used in HTTP for uploads and downloads.
    // 

    // Also, binary MIME is not representable in a string.  
    // If we try to get the MIME as a string, then it must be encoded
    // using base64.

    // Chilkat automatically changes binary encodings to base64
    // when there's an attempt to get the MIME as a string.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoSb to vSb
    Get ComGetMimeSb Of hoEmail vSb To iSuccess
    Get ComWriteFile Of hoSb "qa_output/email_fromSb.eml" "utf-8" False To iSuccess

    // Likewise, if we try to get the MIME as a string from the Mime object, 
    // it cannot contain non-character data in a binary encoding.  The binary
    // bytes MUST be in base64.  The act of trying to retrieve the MIME in string
    // format will force Chilkat to convert binary encodings (for non-text parts)
    // to base64.
    Get pvComObject of hoSb to vSb
    Get ComGetMimeSb Of hoMime vSb To iSuccess
    Get ComWriteFile Of hoSb "qa_output/mime_fromSb.eml" "utf-8" False To iSuccess

    // However, the above use of base64 is just for the purpose of making the MIME
    // string friendly.  If we save the MIME to a file, it's still binary:
    Get ComSaveMime Of hoMime "qa_output/mime_binary.mime" To iSuccess


End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.