Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Create Binary MIMEDemonstrates 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.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @mime int -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @mime, 'SetBodyFromPlainText', @success OUT, 'This is the plain text body.' EXEC sp_OAMethod @mime, 'ConvertToMultipartMixed', @success OUT EXEC sp_OAMethod @mime, 'AppendPartFromFile', @success OUT, 'qa_data/jpg/penguins.jpg' EXEC sp_OAMethod @mime, 'AppendPartFromFile', @success OUT, 'qa_data/pdf/fishing.pdf' -- At this point, when saved, the MIME bodies will be base64 encoded. EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, '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. DECLARE @jpgPart int EXEC sp_OAMethod @mime, 'GetPart', @jpgPart OUT, 1 EXEC sp_OASetProperty @jpgPart, 'Encoding', 'binary' EXEC @hr = sp_OADestroy @jpgPart DECLARE @pdfPart int EXEC sp_OAMethod @mime, 'GetPart', @pdfPart OUT, 2 EXEC sp_OASetProperty @pdfPart, 'Encoding', 'binary' EXEC @hr = sp_OADestroy @pdfPart -- 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. EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, '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? DECLARE @binData int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @binData OUT -- Write the binary MIME into binData; EXEC sp_OAMethod @mime, 'GetMimeBd', @success OUT, @binData DECLARE @email int -- Use "Chilkat_9_5_0.Email" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT -- Load the email from the binData. EXEC sp_OAMethod @email, 'SetFromMimeBd', @success OUT, @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. EXEC sp_OASetProperty @email, 'Subject', 'Binary MIME Email' EXEC sp_OASetProperty @email, 'From', 'admin@chilkatsoft.com' EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Chilkat', 'support@chilkatsoft.com' EXEC sp_OAMethod @email, 'SaveEml', @success OUT, '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. DECLARE @sb int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT EXEC sp_OAMethod @email, 'GetMimeSb', @success OUT, @sb EXEC sp_OAMethod @sb, 'WriteFile', @success OUT, 'qa_output/email_fromSb.eml', 'utf-8', 0 -- 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. EXEC sp_OAMethod @mime, 'GetMimeSb', @success OUT, @sb EXEC sp_OAMethod @sb, 'WriteFile', @success OUT, 'qa_output/mime_fromSb.eml', 'utf-8', 0 -- 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: EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, 'qa_output/mime_binary.mime' EXEC @hr = sp_OADestroy @mime EXEC @hr = sp_OADestroy @binData EXEC @hr = sp_OADestroy @email EXEC @hr = sp_OADestroy @sb END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.