Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Java Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Java) 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 Java Downloads

Java Libs for Windows, MacOS, Linux, Alpine Linux, Solaris

Java Libs for Android

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkMime mime = new CkMime();
    boolean success;

    mime.SetBodyFromPlainText("This is the plain text body.");
    mime.ConvertToMultipartMixed();
    mime.AppendPartFromFile("qa_data/jpg/penguins.jpg");
    mime.AppendPartFromFile("qa_data/pdf/fishing.pdf");

    // At this point, when saved, the MIME bodies will be base64 encoded.
    mime.SaveMime("qa_output/sample.txt");

    // 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.
    CkMime jpgPart = mime.GetPart(1);
    jpgPart.put_Encoding("binary");

    CkMime pdfPart = mime.GetPart(2);
    pdfPart.put_Encoding("binary");

    // 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.
    mime.SaveMime("qa_output/sampleBinary.mim");

    // 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?
    CkBinData binData = new CkBinData();
    // Write the binary MIME into binData;
    mime.GetMimeBd(binData);

    CkEmail email = new CkEmail();
    // Load the email from the binData.
    email.SetFromMimeBd(binData);

    // Note: Many email clients may not be able to correctly process emails
    // using the binary encoding.  Thunderbird has trouble.  Windows Live Mail
    // worked OK.
    email.put_Subject("Binary MIME Email");
    email.put_From("admin@chilkatsoft.com");
    email.AddTo("Chilkat","support@chilkatsoft.com");
    email.SaveEml("qa_output/binaryEmail.eml");

    // 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.
    CkStringBuilder sb = new CkStringBuilder();
    email.GetMimeSb(sb);
    sb.WriteFile("qa_output/email_fromSb.eml","utf-8",false);

    // 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.
    mime.GetMimeSb(sb);
    sb.WriteFile("qa_output/mime_fromSb.eml","utf-8",false);

    // 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:
    mime.SaveMime("qa_output/mime_binary.mime");
  }
}

 

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