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

SQL Server 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
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
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

 

 

 

(SQL Server) Emails with Related Items that have "Attachment" Content-Disposition

Emails are MIME, and an email with attachments or HTML images are multipart MIME. The MIME parts that are items to be displayed inline within an HTML email are called "related" items. Typically they are images. They are situationed under the "multipart/related" MIME umbrella. Attachments are semantically those files that are attached to an email but typically not displayed inline. They are located under the "multipart/mixed" MIME umbrella.

However, related items and attachments aren't always mutually exclusive. Chilkat considers the semantics of the any individual item, not just the placement within the MIME. Also, many software systems create emails in non-standard ways. There are innumerable ways (going against customary "standards") that multipart MIME can be constructed.

For example, if a MS-Word document is located under a multipart/related, but also the MIME sub-header indicates "attachment", then the MS-Word document would be both a related item and an attachment. This is because some email clients are capable of displaying MS-Word docs inline, but also it's semantically an attachment, because in most cases the MS-Word document is expected to be saved to disk.

Here's a sample MIME sub-header. (You can open .eml files in a text editor to see the MIME headers and sub-headers. If you're not familiar with MIME, ask ChatGPT to explain the basics.)

------=_Part_2795453_726600066.1721122066159
Content-Type: application/octet-stream; name="abcd.docx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="abcd.docx"

Note: This example requires Chilkat v10.0.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Load an email with a MS-Word (docx) MIME part that is considered both a related item and an attachment:
    -- 
    -- ... 
    -- ------=_Part_2795453_726600066.1721122066159
    -- Content-Type: application/octet-stream; name="abcd.docx"
    -- Content-Transfer-Encoding: base64
    -- Content-Disposition: attachment; filename="abcd.docx"
    -- ...
    -- 
    DECLARE @success int
    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/related_item_with_disposition_attachment.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    -- Examine related items and attachments:

    EXEC sp_OAGetProperty @email, 'NumRelatedItems', @iTmp0 OUT
    PRINT 'Num Related Items: ' + @iTmp0

    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'Num Attachments: ' + @iTmp0

    -- In our example file, we already know it has 1 related item, and 1 attachment,
    -- and they are both the same MS-Word document.
    -- To simplify for the example, we just get the info for the 1st without checking..

    EXEC sp_OAMethod @email, 'GetRelatedFilename', @sTmp0 OUT, 0
    PRINT 'Related filename: ' + @sTmp0

    EXEC sp_OAMethod @email, 'GetAttachmentFilename', @sTmp0 OUT, 0
    PRINT 'Attachment filename: ' + @sTmp0

    -- The filename is "abcd.docx" in both cases.

    -- Get the Content-Disposition header field.
    -- Note: The GetRelatedHeader method is added in Chilkat v10.0.0
    DECLARE @disposition nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetRelatedHeader', @disposition OUT, 0, 'Content-Disposition'
    EXEC sp_OAGetProperty @email, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    PRINT 'Content-Disposition: ' + @disposition

    -- Sample output:

    -- Num Related Items: 1
    -- Num Attachments: 1
    -- Related filename: abcd.docx
    -- Attachment filename: abcd.docx
    -- Content-Disposition: attachment; filename="abcd.docx

    EXEC @hr = sp_OADestroy @email


END
GO

 

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